Skip to content

Commit 4fb463b

Browse files
authored
[CIR] Lower cir.bool to i1 (#1158)
This PR changes changes the lowering of `cir.bool` to `i1` in both DorectToLLVM and ThroughMLIR. This dramatically simplifies the lowering logic of most operations and the lowered code itself as it naturally uses `i1` for anything boolean. The change involves separating between type lowering when scalars are involved and when memory is involved. This is a pattern that was inspired by clang's codegen which directly emits `i1` from the AST without intermediate higher level representation like CIR has. This also paves the way to more complex lowerings that are implemented in clang codegen through the three primitives added here: `Convert Type For Memory`, `Emit For Memory` and `Emit To Memory`. They are used in clang for non-trivial types like bitints but also extensible vectors.
1 parent 04d7dcf commit 4fb463b

37 files changed

+516
-347
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) {
820820
mlir::Type ptrTy = builder.getPointerTo(OldTy);
821821
mlir::Value cast =
822822
builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy);
823-
UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()});
823+
UseOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp());
824824
}
825825
}
826826
}

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

+190-107
Large diffs are not rendered by default.

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

+45-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
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 {
22+
23+
/// Convert a CIR attribute to an LLVM attribute. May use the datalayout for
24+
/// lowering attributes to-be-stored in memory.
2125
mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::Attribute attr,
2226
mlir::ConversionPatternRewriter &rewriter,
23-
const mlir::TypeConverter *converter);
27+
const mlir::TypeConverter *converter,
28+
mlir::DataLayout const &dataLayout);
2429

2530
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
2631

@@ -137,7 +142,13 @@ class CIRToLLVMMemSetInlineOpLowering
137142

138143
class CIRToLLVMPtrStrideOpLowering
139144
: public mlir::OpConversionPattern<cir::PtrStrideOp> {
145+
mlir::DataLayout const &dataLayout;
146+
140147
public:
148+
CIRToLLVMPtrStrideOpLowering(const mlir::TypeConverter &typeConverter,
149+
mlir::MLIRContext *context,
150+
mlir::DataLayout const &dataLayout)
151+
: OpConversionPattern(typeConverter, context), dataLayout(dataLayout) {}
141152
using mlir::OpConversionPattern<cir::PtrStrideOp>::OpConversionPattern;
142153

143154
mlir::LogicalResult
@@ -216,9 +227,15 @@ class CIRToLLVMBrCondOpLowering
216227
};
217228

218229
class CIRToLLVMCastOpLowering : public mlir::OpConversionPattern<cir::CastOp> {
230+
mlir::DataLayout const &dataLayout;
231+
219232
mlir::Type convertTy(mlir::Type ty) const;
220233

221234
public:
235+
CIRToLLVMCastOpLowering(const mlir::TypeConverter &typeConverter,
236+
mlir::MLIRContext *context,
237+
mlir::DataLayout const &dataLayout)
238+
: OpConversionPattern(typeConverter, context), dataLayout(dataLayout) {}
222239
using mlir::OpConversionPattern<cir::CastOp>::OpConversionPattern;
223240

224241
mlir::LogicalResult
@@ -302,12 +319,15 @@ class CIRToLLVMAllocaOpLowering
302319

303320
class CIRToLLVMLoadOpLowering : public mlir::OpConversionPattern<cir::LoadOp> {
304321
cir::LowerModule *lowerMod;
322+
mlir::DataLayout const &dataLayout;
305323

306324
public:
307325
CIRToLLVMLoadOpLowering(const mlir::TypeConverter &typeConverter,
308326
mlir::MLIRContext *context,
309-
cir::LowerModule *lowerModule)
310-
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule) {}
327+
cir::LowerModule *lowerModule,
328+
mlir::DataLayout const &dataLayout)
329+
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule),
330+
dataLayout(dataLayout) {}
311331

312332
mlir::LogicalResult
313333
matchAndRewrite(cir::LoadOp op, OpAdaptor,
@@ -317,12 +337,15 @@ class CIRToLLVMLoadOpLowering : public mlir::OpConversionPattern<cir::LoadOp> {
317337
class CIRToLLVMStoreOpLowering
318338
: public mlir::OpConversionPattern<cir::StoreOp> {
319339
cir::LowerModule *lowerMod;
340+
mlir::DataLayout const &dataLayout;
320341

321342
public:
322343
CIRToLLVMStoreOpLowering(const mlir::TypeConverter &typeConverter,
323344
mlir::MLIRContext *context,
324-
cir::LowerModule *lowerModule)
325-
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule) {}
345+
cir::LowerModule *lowerModule,
346+
mlir::DataLayout const &dataLayout)
347+
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule),
348+
dataLayout(dataLayout) {}
326349

327350
mlir::LogicalResult
328351
matchAndRewrite(cir::StoreOp op, OpAdaptor,
@@ -332,12 +355,15 @@ class CIRToLLVMStoreOpLowering
332355
class CIRToLLVMConstantOpLowering
333356
: public mlir::OpConversionPattern<cir::ConstantOp> {
334357
cir::LowerModule *lowerMod;
358+
mlir::DataLayout const &dataLayout;
335359

336360
public:
337361
CIRToLLVMConstantOpLowering(const mlir::TypeConverter &typeConverter,
338362
mlir::MLIRContext *context,
339-
cir::LowerModule *lowerModule)
340-
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule) {
363+
cir::LowerModule *lowerModule,
364+
mlir::DataLayout const &dataLayout)
365+
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule),
366+
dataLayout(dataLayout) {
341367
setHasBoundedRewriteRecursion();
342368
}
343369

@@ -538,12 +564,15 @@ class CIRToLLVMSwitchFlatOpLowering
538564
class CIRToLLVMGlobalOpLowering
539565
: public mlir::OpConversionPattern<cir::GlobalOp> {
540566
cir::LowerModule *lowerMod;
567+
mlir::DataLayout const &dataLayout;
541568

542569
public:
543570
CIRToLLVMGlobalOpLowering(const mlir::TypeConverter &typeConverter,
544571
mlir::MLIRContext *context,
545-
cir::LowerModule *lowerModule)
546-
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule) {
572+
cir::LowerModule *lowerModule,
573+
mlir::DataLayout const &dataLayout)
574+
: OpConversionPattern(typeConverter, context), lowerMod(lowerModule),
575+
dataLayout(dataLayout) {
547576
setHasBoundedRewriteRecursion();
548577
}
549578

@@ -904,7 +933,14 @@ class CIRToLLVMTrapOpLowering : public mlir::OpConversionPattern<cir::TrapOp> {
904933

905934
class CIRToLLVMInlineAsmOpLowering
906935
: public mlir::OpConversionPattern<cir::InlineAsmOp> {
936+
mlir::DataLayout const &dataLayout;
937+
907938
public:
939+
CIRToLLVMInlineAsmOpLowering(const mlir::TypeConverter &typeConverter,
940+
mlir::MLIRContext *context,
941+
mlir::DataLayout const &dataLayout)
942+
: OpConversionPattern(typeConverter, context), dataLayout(dataLayout) {}
943+
908944
using mlir::OpConversionPattern<cir::InlineAsmOp>::OpConversionPattern;
909945

910946
mlir::LogicalResult

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRLoopToSCF.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,8 @@ class CIRConditionOpLowering
337337
auto *parentOp = op->getParentOp();
338338
return llvm::TypeSwitch<mlir::Operation *, mlir::LogicalResult>(parentOp)
339339
.Case<mlir::scf::WhileOp>([&](auto) {
340-
auto condition = adaptor.getCondition();
341-
auto i1Condition = rewriter.create<mlir::arith::TruncIOp>(
342-
op.getLoc(), rewriter.getI1Type(), condition);
343340
rewriter.replaceOpWithNewOp<mlir::scf::ConditionOp>(
344-
op, i1Condition, parentOp->getOperands());
341+
op, adaptor.getCondition(), parentOp->getOperands());
345342
return mlir::success();
346343
})
347344
.Default([](auto) { return mlir::failure(); });

0 commit comments

Comments
 (0)