Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/Conversion/ArcToLLVM/LowerArcToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,21 @@ void LowerArcToLLVMPass::runOnOperation() {
return IntegerType::get(type.getContext(), 64);
});

// i0 is not legal in LLVM-IR. Most i0 should have been removed already,
// but if any manage to slip through (e.g. in function signatures), we'll turn
// them into i1 for now.
converter.addConversion([&](IntegerType type) -> Type {
if (type.getWidth() == 0)
return IntegerType::get(type.getContext(), 1);
return type;
});
converter.addSourceMaterialization(
[](OpBuilder &b, IntegerType type, ValueRange, Location loc) -> Value {
if (type.getWidth() != 0)
return nullptr;
return hw::ConstantOp::create(b, loc, APInt::getZero(0));
});

// Setup the conversion patterns.
ConversionPatternSet patterns(&getContext(), converter);

Expand Down
16 changes: 11 additions & 5 deletions lib/Conversion/HWToLLVM/HWToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ struct ArrayInjectOpConversion
auto oneC =
LLVM::ConstantOp::create(rewriter, op->getLoc(), rewriter.getI32Type(),
rewriter.getI32IntegerAttr(1));
auto zextIndex = zextByOne(op->getLoc(), rewriter, op.getIndex());
auto zextIndex = zextByOne(op->getLoc(), rewriter, adaptor.getIndex());

if (arrElems == 1 || !llvm::isPowerOf2_64(arrElems)) {
// Clamp index to prevent OOB access. We add an extra element to the
Expand Down Expand Up @@ -353,7 +353,7 @@ struct ArrayGetOpConversion : public HWArrayOpToLLVMPattern<hw::ArrayGetOp> {

auto arrTy = typeConverter->convertType(op.getInput().getType());
auto elemTy = typeConverter->convertType(op.getResult().getType());
auto zextIndex = zextByOne(op->getLoc(), rewriter, op.getIndex());
auto zextIndex = zextByOne(op->getLoc(), rewriter, adaptor.getIndex());

// During the ongoing migration to opaque types, use the constructor that
// accepts an element type when the array pointer type is opaque, and
Expand Down Expand Up @@ -389,7 +389,7 @@ struct ArraySliceOpConversion
if (!arrPtr)
arrPtr = spillValueOnStack(rewriter, op.getLoc(), adaptor.getInput());

auto zextIndex = zextByOne(op->getLoc(), rewriter, op.getLowIndex());
auto zextIndex = zextByOne(op->getLoc(), rewriter, adaptor.getLowIndex());

// During the ongoing migration to opaque types, use the constructor that
// accepts an element type when the array pointer type is opaque, and
Expand Down Expand Up @@ -503,9 +503,15 @@ struct HWConstantOpConversion : public ConvertToLLVMPattern {
auto constOp = cast<hw::ConstantOp>(op);
// Get the converted llvm type.
auto intType = typeConverter->convertType(constOp.getValueAttr().getType());

Attribute attr = constOp.getValueAttr();
if (auto intAttr = dyn_cast<IntegerAttr>(attr)) {
// Ensure the attribute's type is converted if needed.
attr = IntegerAttr::get(intType, intAttr.getValue());
}

// Replace the operation with an llvm constant op.
rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(op, intType,
constOp.getValueAttr());
rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(op, intType, attr);

return success();
}
Expand Down
18 changes: 16 additions & 2 deletions test/Conversion/ArcToLLVM/lower-arc-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func.func @WriteArray(%arg0: !arc.state<!hw.array<4xi1>>, %arg1: !hw.array<4xi1>
// CHECK-LABEL: llvm.func @DontCrashOnI0(
func.func @DontCrashOnI0(%arg0: i1, %arg1: !hw.array<1xi42>) -> i42 {
// CHECK: [[STACK:%.+]] = llvm.alloca {{%.+}} x !llvm.array<1 x i42>
// CHECK: [[ZERO:%.+]] = llvm.mlir.constant(0 : i0) : i0
// CHECK: [[ZEXT:%.+]] = llvm.zext [[ZERO]] : i0 to i1
// CHECK: [[ZERO:%.+]] = llvm.mlir.constant(false) : i1
// CHECK: [[ZEXT:%.+]] = llvm.zext [[ZERO]] : i1 to i2
// CHECK: [[GEP:%.+]] = llvm.getelementptr [[STACK]][0, [[ZEXT]]] :
// CHECK: [[RESULT:%.+]] = llvm.load [[GEP]] : !llvm.ptr -> i42
// CHECK: llvm.return [[RESULT]]
Expand All @@ -249,6 +249,20 @@ func.func @DontCrashOnI0(%arg0: i1, %arg1: !hw.array<1xi42>) -> i42 {
return %1 : i42
}

// CHECK-LABEL: llvm.func @DontCrashOnI0_2
// CHECK-NOT: i0
func.func @DontCrashOnI0_2(%arg0: i0, %arg1: !hw.array<1xi42>) -> i42 {
%0 = hw.array_get %arg1[%arg0] : !hw.array<1xi42>, i0
return %0 : i42
}

// CHECK-LABEL: llvm.func @DontCrashOnI0_3
// CHECK-NOT: i0
func.func @DontCrashOnI0_3() -> i0 {
%0 = hw.constant 0 : i0
return %0 : i0
}

// CHECK-LABEL: llvm.func @ExecuteEmpty
func.func @ExecuteEmpty() {
// CHECK-NEXT: llvm.br [[BB:\^.+]]
Expand Down
Loading