Skip to content

Commit

Permalink
[CIR][CIRGen] Removes hasBooleanRepresentation (#251)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitoleg authored and lanza committed Sep 7, 2023
1 parent efbd366 commit 7de409e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
6 changes: 2 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// This contains code to emit Expr nodes as CIR code.
//
//===----------------------------------------------------------------------===//

#include "CIRGenBuilder.h"
#include "CIRGenCXXABI.h"
#include "CIRGenCall.h"
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
/// 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<mlir::cir::LoadOp>(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) {
Expand Down
31 changes: 31 additions & 0 deletions clang/test/CIR/CodeGen/bool.c
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>

typedef struct {
bool x;
} S;

// CHECK: cir.func @store_bool
// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
// 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<!ty_22S22>>, !cir.ptr<!ty_22S22>
// CHECK: [[TMP4:%.*]] = cir.get_member [[TMP3]][0] {name = "x"} : !cir.ptr<!ty_22S22> -> !cir.ptr<!cir.bool>
// CHECK: cir.store [[TMP2]], [[TMP4]] : !cir.bool, cir.ptr <!cir.bool>
void store_bool(S *s) {
s->x = false;
}

// CHECK: cir.func @load_bool
// CHECK: [[TMP0:%.*]] = cir.alloca !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>, ["s", init] {alignment = 8 : i64}
// CHECK: [[TMP1:%.*]] = cir.alloca !cir.bool, cir.ptr <!cir.bool>, ["x", init] {alignment = 1 : i64}
// CHECK: cir.store %arg0, [[TMP0]] : !cir.ptr<!ty_22S22>, cir.ptr <!cir.ptr<!ty_22S22>>
// CHECK: [[TMP2:%.*]] = cir.load [[TMP0]] : cir.ptr <!cir.ptr<!ty_22S22>>, !cir.ptr<!ty_22S22>
// CHECK: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "x"} : !cir.ptr<!ty_22S22> -> !cir.ptr<!cir.bool>
// CHECK: [[TMP4:%.*]] = cir.load [[TMP3]] : cir.ptr <!cir.bool>, !cir.bool
void load_bool(S *s) {
bool x = s->x;
}

0 comments on commit 7de409e

Please sign in to comment.