From 7de409e615a9a45a2ffebcdf002c0923e4e32ae1 Mon Sep 17 00:00:00 2001 From: gitoleg Date: Fri, 8 Sep 2023 00:04:01 +0300 Subject: [PATCH] [CIR][CIRGen] Removes hasBooleanRepresentation (#251) This PR removes the method `hasBooleanRepresentation` as was discussed in [PR#233](https://github.com/llvm/clangir/pull/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 --- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 6 ++--- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 5 +--- clang/test/CIR/CodeGen/bool.c | 31 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 clang/test/CIR/CodeGen/bool.c diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 71f296c11662..d82b610520aa 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -9,7 +9,6 @@ // This contains code to emit Expr nodes as CIR code. // //===----------------------------------------------------------------------===// - #include "CIRGenBuilder.h" #include "CIRGenCXXABI.h" #include "CIRGenCall.h" @@ -2214,9 +2213,8 @@ mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue, } mlir::Value CIRGenFunction::buildFromMemory(mlir::Value Value, QualType Ty) { - // Bool has a different representation in memory than in registers. - if (hasBooleanRepresentation(Ty)) { - llvm_unreachable("NYI"); + if (!Ty->isBooleanType() && hasBooleanRepresentation(Ty)) { + llvm_unreachable("NIY"); } return Value; diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 3a136df08054..9eec9aad1d46 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -212,11 +212,8 @@ class ScalarExprEmitter : public StmtVisitor { /// Emits the address of the l-value, then loads and returns the result. mlir::Value buildLoadOfLValue(const Expr *E) { LValue LV = CGF.buildLValue(E); - auto load = Builder.create(CGF.getLoc(E->getExprLoc()), - CGF.getCIRType(E->getType()), - LV.getPointer()); // FIXME: add some akin to EmitLValueAlignmentAssumption(E, V); - return load; + return CGF.buildLoadOfLValue(LV, E->getExprLoc()).getScalarVal(); } mlir::Value buildLoadOfLValue(LValue LV, SourceLocation Loc) { diff --git a/clang/test/CIR/CodeGen/bool.c b/clang/test/CIR/CodeGen/bool.c new file mode 100644 index 000000000000..f1e487f35223 --- /dev/null +++ b/clang/test/CIR/CodeGen/bool.c @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +#include + +typedef struct { + bool x; +} S; + +// CHECK: cir.func @store_bool +// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr, cir.ptr > +// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr, cir.ptr > +// CHECK: [[TMP1:%.*]] = cir.const(#cir.int<0> : !s32i) : !s32i +// CHECK: [[TMP2:%.*]] = cir.cast(int_to_bool, [[TMP1]] : !s32i), !cir.bool +// CHECK: [[TMP3:%.*]] = cir.load [[TMP0]] : cir.ptr >, !cir.ptr +// CHECK: [[TMP4:%.*]] = cir.get_member [[TMP3]][0] {name = "x"} : !cir.ptr -> !cir.ptr +// CHECK: cir.store [[TMP2]], [[TMP4]] : !cir.bool, cir.ptr +void store_bool(S *s) { + s->x = false; +} + +// CHECK: cir.func @load_bool +// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr, cir.ptr >, ["s", init] {alignment = 8 : i64} +// CHECK: [[TMP1:%.*]] = cir.alloca !cir.bool, cir.ptr , ["x", init] {alignment = 1 : i64} +// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr, cir.ptr > +// CHECK: [[TMP2:%.*]] = cir.load [[TMP0]] : cir.ptr >, !cir.ptr +// CHECK: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "x"} : !cir.ptr -> !cir.ptr +// CHECK: [[TMP4:%.*]] = cir.load [[TMP3]] : cir.ptr , !cir.bool +void load_bool(S *s) { + bool x = s->x; +} \ No newline at end of file