Skip to content

Commit 021ebee

Browse files
gitoleglanza
authored andcommitted
[CIR][CIRGen] Removes hasBooleanRepresentation (#251)
This PR removes the method `hasBooleanRepresentation` as was discussed in [PR#233](#233) Briefly, the `cir.bool` has the same representation in memory and after load. The LLVM IR differs, that's why the check is there, in the origin `CodeGen`. Also, in order to trigger the path and make the implementation to be conform with the original `CodeGen`, there are changes in the `CIRGenExprScalar`: call the common `buildFromLValue` instead of manaul `load` creation. Also, a couple of tests for the bool load/store added
1 parent b688cfc commit 021ebee

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// This contains code to emit Expr nodes as CIR code.
1010
//
1111
//===----------------------------------------------------------------------===//
12-
1312
#include "CIRGenBuilder.h"
1413
#include "CIRGenCXXABI.h"
1514
#include "CIRGenCall.h"
@@ -2210,9 +2209,8 @@ mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
22102209
}
22112210

22122211
mlir::Value CIRGenFunction::buildFromMemory(mlir::Value Value, QualType Ty) {
2213-
// Bool has a different representation in memory than in registers.
2214-
if (hasBooleanRepresentation(Ty)) {
2215-
llvm_unreachable("NYI");
2212+
if (!Ty->isBooleanType() && hasBooleanRepresentation(Ty)) {
2213+
llvm_unreachable("NIY");
22162214
}
22172215

22182216
return Value;

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
212212
/// Emits the address of the l-value, then loads and returns the result.
213213
mlir::Value buildLoadOfLValue(const Expr *E) {
214214
LValue LV = CGF.buildLValue(E);
215-
auto load = Builder.create<mlir::cir::LoadOp>(CGF.getLoc(E->getExprLoc()),
216-
CGF.getCIRType(E->getType()),
217-
LV.getPointer());
218215
// FIXME: add some akin to EmitLValueAlignmentAssumption(E, V);
219-
return load;
216+
return CGF.buildLoadOfLValue(LV, E->getExprLoc()).getScalarVal();
220217
}
221218

222219
mlir::Value buildLoadOfLValue(LValue LV, SourceLocation Loc) {

clang/test/CIR/CodeGen/bool.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s
3+
4+
#include <stdbool.h>
5+
6+
typedef struct {
7+
bool x;
8+
} S;
9+
10+
// CHECK: cir.func @store_bool
11+
// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
12+
// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
13+
// CHECK: [[TMP1:%.*]] = cir.const(#cir.int<0> : !s32i) : !s32i
14+
// CHECK: [[TMP2:%.*]] = cir.cast(int_to_bool, [[TMP1]] : !s32i), !cir.bool
15+
// CHECK: [[TMP3:%.*]] = cir.load [[TMP0]] : cir.ptr <!cir.ptr<!ty_22S22>>, !cir.ptr<!ty_22S22>
16+
// CHECK: [[TMP4:%.*]] = cir.get_member [[TMP3]][0] {name = "x"} : !cir.ptr<!ty_22S22> -> !cir.ptr<!cir.bool>
17+
// CHECK: cir.store [[TMP2]], [[TMP4]] : !cir.bool, cir.ptr <!cir.bool>
18+
void store_bool(S *s) {
19+
s->x = false;
20+
}
21+
22+
// CHECK: cir.func @load_bool
23+
// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>, ["s", init] {alignment = 8 : i64}
24+
// CHECK: [[TMP1:%.*]] = cir.alloca !cir.bool, cir.ptr <!cir.bool>, ["x", init] {alignment = 1 : i64}
25+
// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
26+
// CHECK: [[TMP2:%.*]] = cir.load [[TMP0]] : cir.ptr <!cir.ptr<!ty_22S22>>, !cir.ptr<!ty_22S22>
27+
// CHECK: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "x"} : !cir.ptr<!ty_22S22> -> !cir.ptr<!cir.bool>
28+
// CHECK: [[TMP4:%.*]] = cir.load [[TMP3]] : cir.ptr <!cir.bool>, !cir.bool
29+
void load_bool(S *s) {
30+
bool x = s->x;
31+
}

0 commit comments

Comments
 (0)