Skip to content

Commit 8fe8f86

Browse files
committed
[CIR][DirectToLLVM] Lower cir.bool to i1
1 parent 8c4c3ca commit 8fe8f86

20 files changed

+272
-204
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 155 additions & 98 deletions
Large diffs are not rendered by default.

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1515
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1616
#include "mlir/IR/MLIRContext.h"
17+
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1718
#include "mlir/Transforms/DialectConversion.h"
1819

1920
namespace cir {
2021
namespace direct {
2122
mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::Attribute attr,
2223
mlir::ConversionPatternRewriter &rewriter,
23-
const mlir::TypeConverter *converter);
24+
const mlir::TypeConverter *converter,
25+
mlir::DataLayout const &dataLayout);
2426

2527
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
2628

@@ -271,7 +273,15 @@ class CIRToLLVMAllocaOpLowering
271273
};
272274

273275
class CIRToLLVMLoadOpLowering : public mlir::OpConversionPattern<cir::LoadOp> {
276+
mlir::DataLayout const &dataLayout;
277+
274278
public:
279+
CIRToLLVMLoadOpLowering(mlir::TypeConverter const &typeConverter,
280+
mlir::DataLayout const &dataLayout,
281+
mlir::MLIRContext *context)
282+
: OpConversionPattern<cir::LoadOp>(typeConverter, context),
283+
dataLayout(dataLayout) {}
284+
275285
using mlir::OpConversionPattern<cir::LoadOp>::OpConversionPattern;
276286

277287
mlir::LogicalResult
@@ -281,7 +291,15 @@ class CIRToLLVMLoadOpLowering : public mlir::OpConversionPattern<cir::LoadOp> {
281291

282292
class CIRToLLVMStoreOpLowering
283293
: public mlir::OpConversionPattern<cir::StoreOp> {
294+
mlir::DataLayout const &dataLayout;
295+
284296
public:
297+
CIRToLLVMStoreOpLowering(mlir::TypeConverter const &typeConverter,
298+
mlir::DataLayout const &dataLayout,
299+
mlir::MLIRContext *context)
300+
: OpConversionPattern<cir::StoreOp>(typeConverter, context),
301+
dataLayout(dataLayout) {}
302+
285303
using mlir::OpConversionPattern<cir::StoreOp>::OpConversionPattern;
286304

287305
mlir::LogicalResult
@@ -291,7 +309,14 @@ class CIRToLLVMStoreOpLowering
291309

292310
class CIRToLLVMConstantOpLowering
293311
: public mlir::OpConversionPattern<cir::ConstantOp> {
312+
mlir::DataLayout const &dataLayout;
313+
294314
public:
315+
CIRToLLVMConstantOpLowering(mlir::TypeConverter const &typeConverter,
316+
mlir::DataLayout const &dataLayout,
317+
mlir::MLIRContext *context)
318+
: OpConversionPattern<cir::ConstantOp>(typeConverter, context),
319+
dataLayout(dataLayout) {}
295320
using mlir::OpConversionPattern<cir::ConstantOp>::OpConversionPattern;
296321

297322
mlir::LogicalResult
@@ -490,7 +515,15 @@ class CIRToLLVMSwitchFlatOpLowering
490515

491516
class CIRToLLVMGlobalOpLowering
492517
: public mlir::OpConversionPattern<cir::GlobalOp> {
518+
mlir::DataLayout const &dataLayout;
519+
493520
public:
521+
CIRToLLVMGlobalOpLowering(mlir::TypeConverter const &typeConverter,
522+
mlir::DataLayout const &dataLayout,
523+
mlir::MLIRContext *context)
524+
: OpConversionPattern<cir::GlobalOp>(typeConverter, context),
525+
dataLayout(dataLayout) {}
526+
494527
using mlir::OpConversionPattern<cir::GlobalOp>::OpConversionPattern;
495528

496529
mlir::LogicalResult

clang/test/CIR/CodeGen/atomic-xchg-field.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ void structAtomicExchange(unsigned referenceCount, wPtr item) {
5858
// LLVM: %[[RES:.*]] = cmpxchg weak ptr %9, i32 %[[EXP]], i32 %[[DES]] seq_cst seq_cst
5959
// LLVM: %[[OLD:.*]] = extractvalue { i32, i1 } %[[RES]], 0
6060
// LLVM: %[[CMP:.*]] = extractvalue { i32, i1 } %[[RES]], 1
61-
// LLVM: %[[Z:.*]] = zext i1 %[[CMP]] to i8, !dbg !16
62-
// LLVM: %[[X:.*]] = xor i8 %[[Z]], 1, !dbg !16
63-
// LLVM: %[[FAIL:.*]] = trunc i8 %[[X]] to i1, !dbg !16
64-
61+
// LLVM: %[[FAIL:.*]] = xor i1 %[[CMP]], true, !dbg !16
6562
// LLVM: br i1 %[[FAIL:.*]], label %[[STORE_OLD:.*]], label %[[CONTINUE:.*]],
6663
// LLVM: [[STORE_OLD]]:
6764
// LLVM: store i32 %[[OLD]], ptr
6865
// LLVM: br label %[[CONTINUE]]
6966

7067
// LLVM: [[CONTINUE]]:
68+
// LLVM: %[[Z:.*]] = zext i1 %[[CMP]] to i8
7169
// LLVM: store i8 %[[Z]], ptr {{.*}}, align 1
7270
// LLVM: ret void
7371

clang/test/CIR/CodeGen/bf16-ops.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ void foo(void) {
4141
// NATIVE-NEXT: %{{.+}} = cir.cast(integral, %[[#C]] : !s32i), !u32i
4242

4343
// NONATIVE-LLVM: %[[#A:]] = fcmp une bfloat %{{.+}}, 0xR0000
44-
// NONATIVE-LLVM-NEXT: %[[#B:]] = zext i1 %[[#A]] to i8
45-
// NONATIVE-LLVM-NEXT: %[[#C:]] = xor i8 %[[#B]], 1
46-
// NONATIVE-LLVM-NEXT: %{{.+}} = zext i8 %[[#C]] to i32
44+
// NONATIVE-LLVM-NEXT: %[[#C:]] = xor i1 %[[#A]], true
45+
// NONATIVE-LLVM-NEXT: %{{.+}} = zext i1 %[[#C]] to i32
4746

4847
// NATIVE-LLVM: %[[#A:]] = fcmp une bfloat %{{.+}}, 0xR0000
49-
// NATIVE-LLVM-NEXT: %[[#B:]] = zext i1 %[[#A]] to i8
50-
// NATIVE-LLVM-NEXT: %[[#C:]] = xor i8 %[[#B]], 1
51-
// NATIVE-LLVM-NEXT: %{{.+}} = zext i8 %[[#C]] to i32
48+
// NATIVE-LLVM-NEXT: %[[#C:]] = xor i1 %[[#A]], true
49+
// NATIVE-LLVM-NEXT: %{{.+}} = zext i1 %[[#C]] to i32
5250

5351
h1 = -h1;
5452
// NONATIVE: %[[#A:]] = cir.cast(floating, %{{.+}} : !cir.bf16), !cir.float

clang/test/CIR/CodeGen/builtin-assume.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int test_assume(int x) {
1616
// CIR: }
1717

1818
// LLVM: @_Z11test_assumei
19-
// LLVM: %[[#cond:]] = trunc i8 %{{.+}} to i1
19+
// LLVM: %[[#cond:]] = icmp sgt i32 %{{.+}}, 0
2020
// LLVM-NEXT: call void @llvm.assume(i1 %[[#cond]])
2121

2222
int test_assume_aligned(int *ptr) {

clang/test/CIR/CodeGen/builtin-constant-p.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ int foo() {
2020
// LLVM: [[TMP1:%.*]] = alloca i32, i64 1
2121
// LLVM: [[TMP2:%.*]] = load i32, ptr @a
2222
// LLVM: [[TMP3:%.*]] = call i1 @llvm.is.constant.i32(i32 [[TMP2]])
23-
// LLVM: [[TMP4:%.*]] = zext i1 [[TMP3]] to i8
24-
// LLVM: [[TMP5:%.*]] = zext i8 [[TMP4]] to i32
23+
// LLVM: [[TMP5:%.*]] = zext i1 [[TMP3]] to i32
2524
// LLVM: store i32 [[TMP5]], ptr [[TMP1]]
2625
// LLVM: [[TMP6:%.*]] = load i32, ptr [[TMP1]]
2726
// LLVM: ret i32 [[TMP6]]

clang/test/CIR/CodeGen/complex-arithmetic.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,9 @@ void mul() {
303303
// LLVM-FULL-NEXT: %[[#F:]] = fadd double %[[#C]], %[[#D]]
304304
// LLVM-FULL-NEXT: %[[#G:]] = insertvalue { double, double } undef, double %[[#E]], 0
305305
// LLVM-FULL-NEXT: %[[#RES:]] = insertvalue { double, double } %[[#G]], double %[[#F]], 1
306-
// LLVM-FULL-NEXT: %[[#H:]] = fcmp une double %[[#E]], %[[#E]]
307-
// LLVM-FULL-NEXT: %[[#COND:]] = zext i1 %[[#H]] to i8
308-
// LLVM-FULL-NEXT: %[[#I:]] = fcmp une double %[[#F]], %[[#F]]
309-
// LLVM-FULL-NEXT: %[[#COND2:]] = zext i1 %[[#I]] to i8
310-
// LLVM-FULL-NEXT: %[[#J:]] = and i8 %[[#COND]], %[[#COND2]]
311-
// LLVM-FULL-NEXT: %[[#COND3:]] = trunc i8 %[[#J]] to i1
306+
// LLVM-FULL-NEXT: %[[#COND:]] = fcmp une double %[[#E]], %[[#E]]
307+
// LLVM-FULL-NEXT: %[[#COND2:]] = fcmp une double %[[#F]], %[[#F]]
308+
// LLVM-FULL-NEXT: %[[#COND3:]] = and i1 %[[#COND]], %[[#COND2]]
312309
// LLVM-FULL: {{.+}}:
313310
// LLVM-FULL-NEXT: %{{.+}} = call { double, double } @__muldc3(double %[[#LHSR]], double %[[#LHSI]], double %[[#RHSR]], double %[[#RHSI]])
314311
// LLVM-FULL-NEXT: br label %{{.+}}

clang/test/CIR/CodeGen/complex-cast.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ void complex_to_bool() {
179179
// LLVM: %[[#REAL:]] = extractvalue { double, double } %{{.+}}, 0
180180
// LLVM-NEXT: %[[#IMAG:]] = extractvalue { double, double } %{{.+}}, 1
181181
// LLVM-NEXT: %[[#RB:]] = fcmp une double %[[#REAL]], 0.000000e+00
182-
// LLVM-NEXT: %[[#RB2:]] = zext i1 %[[#RB]] to i8
183182
// LLVM-NEXT: %[[#IB:]] = fcmp une double %[[#IMAG]], 0.000000e+00
184-
// LLVM-NEXT: %[[#IB2:]] = zext i1 %[[#IB]] to i8
185-
// LLVM-NEXT: %{{.+}} = or i8 %[[#RB2]], %[[#IB2]]
183+
// LLVM-NEXT: %{{.+}} = or i1 %[[#RB]], %[[#IB]]
186184

187185
// CIR-BEFORE: %{{.+}} = cir.cast(int_complex_to_bool, %{{.+}} : !cir.complex<!s32i>), !cir.bool
188186

@@ -196,10 +194,8 @@ void complex_to_bool() {
196194
// LLVM: %[[#REAL:]] = extractvalue { i32, i32 } %{{.+}}, 0
197195
// LLVM-NEXT: %[[#IMAG:]] = extractvalue { i32, i32 } %{{.+}}, 1
198196
// LLVM-NEXT: %[[#RB:]] = icmp ne i32 %[[#REAL]], 0
199-
// LLVM-NEXT: %[[#RB2:]] = zext i1 %[[#RB]] to i8
200197
// LLVM-NEXT: %[[#IB:]] = icmp ne i32 %[[#IMAG]], 0
201-
// LLVM-NEXT: %[[#IB2:]] = zext i1 %[[#IB]] to i8
202-
// LLVM-NEXT: %{{.+}} = or i8 %[[#RB2]], %[[#IB2]]
198+
// LLVM-NEXT: %{{.+}} = or i1 %[[#RB]], %[[#IB]]
203199

204200
// CHECK: }
205201

clang/test/CIR/CodeGen/new-null.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace test15 {
6666
// LLVM: %[[VAL_0:.*]] = alloca ptr, i64 1, align 8
6767
// LLVM: store ptr %[[VAL_1:.*]], ptr %[[VAL_0]], align 8
6868
// LLVM: %[[VAL_2:.*]] = load ptr, ptr %[[VAL_0]], align 8
69-
// LLVM: %[[VAL_3:.*]] = call ptr @_ZnwmPvb(i64 1, ptr %[[VAL_2]], i8 1)
69+
// LLVM: %[[VAL_3:.*]] = call ptr @_ZnwmPvb(i64 1, ptr %[[VAL_2]], i1 true)
7070
// LLVM: %[[VAL_4:.*]] = icmp ne ptr %[[VAL_3]], null
7171
// LLVM: br i1 %[[VAL_4]], label %[[VAL_5:.*]], label %[[VAL_6:.*]],
7272
// LLVM: [[VAL_5]]: ; preds = %[[VAL_7:.*]]

clang/test/CIR/CodeGen/static.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ static Init __ioinit2(false);
7777
// LLVM: @_ZL9__ioinit2 = internal global %class.Init zeroinitializer
7878
// LLVM: @llvm.global_ctors = appending constant [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init, ptr null }, { i32, ptr, ptr } { i32 65536, ptr @__cxx_global_var_init.1, ptr null }]
7979
// LLVM: define internal void @__cxx_global_var_init()
80-
// LLVM-NEXT: call void @_ZN4InitC1Eb(ptr @_ZL8__ioinit, i8 1)
80+
// LLVM-NEXT: call void @_ZN4InitC1Eb(ptr @_ZL8__ioinit, i1 true)
8181
// LLVM-NEXT: call void @__cxa_atexit(ptr @_ZN4InitD1Ev, ptr @_ZL8__ioinit, ptr @__dso_handle)
8282
// LLVM-NEXT: ret void
8383
// LLVM: define internal void @__cxx_global_var_init.1()
84-
// LLVM-NEXT: call void @_ZN4InitC1Eb(ptr @_ZL9__ioinit2, i8 0)
84+
// LLVM-NEXT: call void @_ZN4InitC1Eb(ptr @_ZL9__ioinit2, i1 false)
8585
// LLVM-NEXT: call void @__cxa_atexit(ptr @_ZN4InitD1Ev, ptr @_ZL9__ioinit2, ptr @__dso_handle)
8686
// LLVM-NEXT: ret void
8787
// LLVM: define void @_GLOBAL__sub_I_static.cpp()

0 commit comments

Comments
 (0)