Skip to content

Commit fb0c1b1

Browse files
committed
[CIR][NFC] Be a bit more strict on lowering logic and random cleanups
More tests for these comming soon
1 parent 8a027e8 commit fb0c1b1

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ mlir::Value CIRGenBuilderTy::promoteArrayIndex(const clang::TargetInfo &ti,
4242

4343
// If this an integer, ensure that it is at least as width as the array index
4444
// type.
45-
if (auto intTy = mlir::dyn_cast<cir::IntType>(index.getType())) {
45+
if (auto intTy = mlir::dyn_cast<cir::IntType>(index.getType()))
4646
if (intTy.getWidth() < arrayIndexWidth)
4747
return cir::CastOp::create(*this, loc, arrayIndexType,
4848
cir::CastKind::integral, index);
49-
}
5049

5150
return index;
5251
}
@@ -57,17 +56,17 @@ mlir::Value CIRGenBuilderTy::getArrayElement(const clang::TargetInfo &ti,
5756
mlir::Value arrayPtr,
5857
mlir::Type eltTy, mlir::Value idx,
5958
bool shouldDecay) {
60-
auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType());
61-
assert(arrayPtrTy && "expected pointer type");
62-
6359
// If the array pointer is not decayed, emit a GetElementOp.
64-
auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
65-
if (shouldDecay && arrayTy && arrayTy == eltTy) {
66-
auto eltPtrTy =
67-
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
68-
return cir::GetElementOp::create(*this, arrayLocEnd, eltPtrTy, arrayPtr,
69-
promoteArrayIndex(ti, arrayLocBegin, idx));
70-
}
60+
if (shouldDecay)
61+
if (auto arrayPtrTy = dyn_cast<cir::PointerType>(arrayPtr.getType()))
62+
if (auto arrayTy = dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee()))
63+
if (arrayTy == eltTy) {
64+
auto eltPtrTy =
65+
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
66+
return cir::GetElementOp::create(
67+
*this, arrayLocEnd, eltPtrTy, arrayPtr,
68+
promoteArrayIndex(ti, arrayLocBegin, idx));
69+
}
7170

7271
// If we don't have sufficient type information, emit a PtrStrideOp.
7372
mlir::Value basePtr = arrayPtr;

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4119,7 +4119,7 @@ LogicalResult cir::GetMethodOp::verify() {
41194119
//===----------------------------------------------------------------------===//
41204120

41214121
LogicalResult cir::GetElementOp::verify() {
4122-
auto arrayTy = mlir::cast<cir::ArrayType>(getBaseType().getPointee());
4122+
auto arrayTy = cast<ArrayType>(getBaseType().getPointee());
41234123
if (getElementType() != arrayTy.getElementType())
41244124
return emitError() << "element type mismatch";
41254125

clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ void LifetimeCheckPass::updatePointsTo(mlir::Value addr, mlir::Value data,
12531253
return;
12541254
}
12551255

1256-
if (auto getElemOp = mlir::dyn_cast<cir::GetElementOp>(dataSrcOp)) {
1256+
if (auto getElemOp = dyn_cast<GetElementOp>(dataSrcOp)) {
12571257
getPmap()[addr].clear();
12581258
getPmap()[addr].insert(State::getLocalValue(getElemOp.getBase()));
12591259
return;
@@ -1960,7 +1960,7 @@ void LifetimeCheckPass::dumpPmap(PMapType &pmap) {
19601960
int entry = 0;
19611961
for (auto &mapEntry : pmap) {
19621962
llvm::errs() << " " << entry << ": " << getVarNameFromValue(mapEntry.first)
1963-
<< " => ";
1963+
<< " " << "=> ";
19641964
printPset(mapEntry.second);
19651965
llvm::errs() << "\n";
19661966
entry++;

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,15 +1132,40 @@ mlir::LogicalResult CIRToLLVMGetElementOpLowering::matchAndRewrite(
11321132
mlir::isa<mlir::LLVM::LLVMFunctionType>(elementTy))
11331133
elementTy = mlir::IntegerType::get(ctx, 8, mlir::IntegerType::Signless);
11341134

1135-
// Zero-extend, sign-extend or trunc the index value.
1135+
// Zero-extend, sign-extend or trunc the pointer value.
11361136
auto index = adaptor.getIndex();
1137+
auto width = mlir::cast<mlir::IntegerType>(index.getType()).getWidth();
11371138
mlir::DataLayout LLVMLayout(op->getParentOfType<mlir::ModuleOp>());
1138-
if (auto layoutWidth =
1139-
LLVMLayout.getTypeIndexBitwidth(adaptor.getBase().getType())) {
1140-
bool isUnsigned = false;
1141-
if (auto strideTy = dyn_cast<cir::IntType>(op.getOperand(1).getType()))
1142-
isUnsigned = strideTy.isUnsigned();
1143-
index = promoteIndex(rewriter, index, *layoutWidth, isUnsigned);
1139+
auto layoutWidth =
1140+
LLVMLayout.getTypeIndexBitwidth(adaptor.getBase().getType());
1141+
auto indexOp = index.getDefiningOp();
1142+
if (indexOp && layoutWidth && width != *layoutWidth) {
1143+
// If the index comes from a subtraction, make sure the extension happens
1144+
// before it. To achieve that, look at unary minus, which already got
1145+
// lowered to "sub 0, x".
1146+
auto sub = dyn_cast<mlir::LLVM::SubOp>(indexOp);
1147+
auto unary =
1148+
dyn_cast_if_present<cir::UnaryOp>(op.getIndex().getDefiningOp());
1149+
bool rewriteSub =
1150+
unary && unary.getKind() == cir::UnaryOpKind::Minus && sub;
1151+
if (rewriteSub)
1152+
index = indexOp->getOperand(1);
1153+
1154+
// Handle the cast
1155+
auto llvmDstType = mlir::IntegerType::get(ctx, *layoutWidth);
1156+
index = getLLVMIntCast(rewriter, index, llvmDstType,
1157+
op.getIndex().getType().isUnsigned(), width,
1158+
*layoutWidth);
1159+
1160+
// Rewrite the sub in front of extensions/trunc
1161+
if (rewriteSub) {
1162+
index = mlir::LLVM::SubOp::create(
1163+
rewriter, index.getLoc(),
1164+
mlir::LLVM::ConstantOp::create(rewriter, index.getLoc(),
1165+
index.getType(), 0),
1166+
index);
1167+
rewriter.eraseOp(sub);
1168+
}
11441169
}
11451170

11461171
// Since the base address is a pointer to an aggregate, the first
@@ -3815,6 +3840,8 @@ CIRToLLVMAtomicFetchLowering::getLLVMBinop(cir::AtomicFetchKind k,
38153840
case cir::AtomicFetchKind::Max:
38163841
case cir::AtomicFetchKind::Min:
38173842
llvm_unreachable("handled in buildMinMaxPostOp");
3843+
case cir::AtomicFetchKind::Xchg:
3844+
llvm_unreachable("xchg has no binop");
38183845
}
38193846
llvm_unreachable("Unknown atomic fetch opcode");
38203847
}

0 commit comments

Comments
 (0)