Skip to content

Commit 9a951af

Browse files
authored
[CIR][CodeGen][LowerToLLVM] Fix llvm lowering of CIR UnaryOpKind_Not (#1194)
Basically, for int type, the order of Ops is not the same as OG in the emitted LLVM IR. OG has constant as the second op position. See [OG's order ](https://godbolt.org/z/584jrWeYn).
1 parent eacaabb commit 9a951af

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,28 +2326,28 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
23262326
}
23272327
case cir::UnaryOpKind::Not: {
23282328
// bit-wise compliment operator, implemented as an XOR with -1.
2329-
mlir::Value MinusOne;
2329+
mlir::Value minusOne;
23302330
if (IsVector) {
23312331
// Creating a vector object with all -1 values is easier said than
23322332
// done. It requires a series of insertelement ops.
23332333
mlir::Type llvmElementType =
23342334
getTypeConverter()->convertType(elementType);
23352335
auto MinusOneInt = rewriter.create<mlir::LLVM::ConstantOp>(
23362336
loc, llvmElementType, mlir::IntegerAttr::get(llvmElementType, -1));
2337-
MinusOne = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmType);
2337+
minusOne = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmType);
23382338
auto NumElements = mlir::dyn_cast<cir::VectorType>(type).getSize();
23392339
for (uint64_t i = 0; i < NumElements; ++i) {
23402340
mlir::Value indexValue = rewriter.create<mlir::LLVM::ConstantOp>(
23412341
loc, rewriter.getI64Type(), i);
2342-
MinusOne = rewriter.create<mlir::LLVM::InsertElementOp>(
2343-
loc, MinusOne, MinusOneInt, indexValue);
2342+
minusOne = rewriter.create<mlir::LLVM::InsertElementOp>(
2343+
loc, minusOne, MinusOneInt, indexValue);
23442344
}
23452345
} else {
2346-
MinusOne = rewriter.create<mlir::LLVM::ConstantOp>(
2346+
minusOne = rewriter.create<mlir::LLVM::ConstantOp>(
23472347
loc, llvmType, mlir::IntegerAttr::get(llvmType, -1));
23482348
}
2349-
rewriter.replaceOpWithNewOp<mlir::LLVM::XOrOp>(op, llvmType, MinusOne,
2350-
adaptor.getInput());
2349+
rewriter.replaceOpWithNewOp<mlir::LLVM::XOrOp>(
2350+
op, llvmType, adaptor.getInput(), minusOne);
23512351
return mlir::success();
23522352
}
23532353
}

clang/test/CIR/CodeGen/vectype-ext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void vector_int_test(int x) {
134134
vi4 n = ~a;
135135
// CIR: %{{[0-9]+}} = cir.unary(not, %{{[0-9]+}}) : !cir.vector<!s32i x 4>, !cir.vector<!s32i x 4>
136136
// LLVM: %[[#VAL:]] = load <4 x i32>, ptr %{{[0-9]+}}, align 16
137-
// LLVM-NEXT: %[[#RES:]] = xor <4 x i32> splat (i32 -1), %[[#VAL]]
137+
// LLVM-NEXT: %[[#RES:]] = xor <4 x i32> %[[#VAL]], splat (i32 -1)
138138
// LLVM-NEXT: store <4 x i32> %[[#RES]], ptr %{{[0-9]+}}, align 16
139139

140140
// TODO: Ternary conditional operator

clang/test/CIR/Lowering/unary-not.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module {
1818
// MLIR: = llvm.mlir.constant(-1 : i32)
1919
// MLIR: = llvm.xor
2020

21-
// LLVM: = xor i32 -1, %[[#]]
21+
// LLVM: = xor i32 %[[#]], -1
2222

2323

2424
cir.func @floatingPoint(%arg0: !cir.float, %arg1: !cir.double) {

clang/test/CIR/Lowering/vectype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void vector_int_test(int x) {
137137
// CHECK: %[[#T101:]] = llvm.insertelement %[[#T94]], %[[#T99]][%[[#T100]] : i64] : vector<4xi32>
138138
// CHECK: %[[#T102:]] = llvm.mlir.constant(3 : i64) : i64
139139
// CHECK: %[[#T103:]] = llvm.insertelement %[[#T94]], %[[#T101]][%[[#T102]] : i64] : vector<4xi32>
140-
// CHECK: %[[#T104:]] = llvm.xor %[[#T103]], %[[#T93]] : vector<4xi32>
140+
// CHECK: %[[#T104:]] = llvm.xor %[[#T93]], %[[#T103]] : vector<4xi32>
141141
// CHECK: llvm.store %[[#T104]], %[[#T29:]] {alignment = 16 : i64} : vector<4xi32>, !llvm.ptr
142142

143143
// Ternary conditional operator

0 commit comments

Comments
 (0)