[clang] [CIR][NFC] Use Op::create to create CIR operations in CIRGenBuilder (PR #154540)
Sirui Mu via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 20 07:44:45 PDT 2025
https://github.com/Lancern updated https://github.com/llvm/llvm-project/pull/154540
>From dc46a49bbfafe220f7006bbccd08212b26dae890 Mon Sep 17 00:00:00 2001
From: Sirui Mu <msrlancern at gmail.com>
Date: Wed, 20 Aug 2025 21:42:09 +0800
Subject: [PATCH] [CIR][NFC] Use Op::create to create CIR operations in
CIRGenBuilder
This patch updates `CIRGenBuilderTy` and `CIRBaseBuilderTy` to use `Op::create`
for creating CIR operations. Compared to the new way which calls
`OpBuilder::create` to create operations, the new way is more friendly to
language servers and developers.
---
.../CIR/Dialect/Builder/CIRBaseBuilder.h | 71 ++++++++++---------
clang/lib/CIR/CodeGen/CIRGenBuilder.h | 37 +++++-----
2 files changed, 57 insertions(+), 51 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 2621139727011..d29e5687d2544 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -63,11 +63,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
const llvm::APInt &val) {
- return create<cir::ConstantOp>(loc, cir::IntAttr::get(typ, val));
+ return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(typ, val));
}
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr) {
- return create<cir::ConstantOp>(loc, attr);
+ return cir::ConstantOp::create(*this, loc, attr);
}
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty,
@@ -119,7 +119,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
}
cir::ConstantOp getBool(bool state, mlir::Location loc) {
- return create<cir::ConstantOp>(loc, getCIRBoolAttr(state));
+ return cir::ConstantOp::create(*this, loc, getCIRBoolAttr(state));
}
cir::ConstantOp getFalse(mlir::Location loc) { return getBool(false, loc); }
cir::ConstantOp getTrue(mlir::Location loc) { return getBool(true, loc); }
@@ -144,17 +144,20 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real,
mlir::Value imag) {
auto resultComplexTy = cir::ComplexType::get(real.getType());
- return create<cir::ComplexCreateOp>(loc, resultComplexTy, real, imag);
+ return cir::ComplexCreateOp::create(*this, loc, resultComplexTy, real,
+ imag);
}
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand) {
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
- return create<cir::ComplexRealOp>(loc, operandTy.getElementType(), operand);
+ return cir::ComplexRealOp::create(*this, loc, operandTy.getElementType(),
+ operand);
}
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
- return create<cir::ComplexImagOp>(loc, operandTy.getElementType(), operand);
+ return cir::ComplexImagOp::create(*this, loc, operandTy.getElementType(),
+ operand);
}
cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr,
@@ -171,7 +174,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
}
mlir::Value createNot(mlir::Value value) {
- return create<cir::UnaryOp>(value.getLoc(), value.getType(),
+ return cir::UnaryOp::create(*this, value.getLoc(), value.getType(),
cir::UnaryOpKind::Not, value);
}
@@ -180,7 +183,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Location loc,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
- return create<cir::DoWhileOp>(loc, condBuilder, bodyBuilder);
+ return cir::DoWhileOp::create(*this, loc, condBuilder, bodyBuilder);
}
/// Create a while operation.
@@ -188,7 +191,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Location loc,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
- return create<cir::WhileOp>(loc, condBuilder, bodyBuilder);
+ return cir::WhileOp::create(*this, loc, condBuilder, bodyBuilder);
}
/// Create a for operation.
@@ -197,22 +200,23 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder,
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> stepBuilder) {
- return create<cir::ForOp>(loc, condBuilder, bodyBuilder, stepBuilder);
+ return cir::ForOp::create(*this, loc, condBuilder, bodyBuilder,
+ stepBuilder);
}
/// Create a break operation.
cir::BreakOp createBreak(mlir::Location loc) {
- return create<cir::BreakOp>(loc);
+ return cir::BreakOp::create(*this, loc);
}
/// Create a continue operation.
cir::ContinueOp createContinue(mlir::Location loc) {
- return create<cir::ContinueOp>(loc);
+ return cir::ContinueOp::create(*this, loc);
}
mlir::Value createUnaryOp(mlir::Location loc, cir::UnaryOpKind kind,
mlir::Value operand) {
- return create<cir::UnaryOp>(loc, kind, operand);
+ return cir::UnaryOp::create(*this, loc, kind, operand);
}
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value) {
@@ -222,7 +226,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
mlir::Type type, llvm::StringRef name,
mlir::IntegerAttr alignment) {
- return create<cir::AllocaOp>(loc, addrType, type, name, alignment);
+ return cir::AllocaOp::create(*this, loc, addrType, type, name, alignment);
}
/// Get constant address of a global variable as an MLIR attribute.
@@ -235,8 +239,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global) {
assert(!cir::MissingFeatures::addressSpace());
- return create<cir::GetGlobalOp>(loc, getPointerTo(global.getSymType()),
- global.getSymName());
+ return cir::GetGlobalOp::create(
+ *this, loc, getPointerTo(global.getSymType()), global.getSymName());
}
mlir::Value createGetGlobal(cir::GlobalOp global) {
@@ -263,7 +267,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy,
mlir::Value base, llvm::StringRef name,
unsigned index) {
- return create<cir::GetMemberOp>(loc, resultTy, base, name, index);
+ return cir::GetMemberOp::create(*this, loc, resultTy, base, name, index);
}
mlir::Value createDummyValue(mlir::Location loc, mlir::Type type,
@@ -276,7 +280,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base,
mlir::Value stride) {
- return create<cir::PtrStrideOp>(loc, base.getType(), base, stride);
+ return cir::PtrStrideOp::create(*this, loc, base.getType(), base, stride);
}
//===--------------------------------------------------------------------===//
@@ -286,7 +290,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
mlir::Type returnType, mlir::ValueRange operands,
llvm::ArrayRef<mlir::NamedAttribute> attrs = {}) {
- auto op = create<cir::CallOp>(loc, callee, returnType, operands);
+ auto op = cir::CallOp::create(*this, loc, callee, returnType, operands);
op->setAttrs(attrs);
return op;
}
@@ -317,7 +321,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value src, mlir::Type newTy) {
if (newTy == src.getType())
return src;
- return create<cir::CastOp>(loc, newTy, kind, src);
+ return cir::CastOp::create(*this, loc, newTy, kind, src);
}
mlir::Value createCast(cir::CastKind kind, mlir::Value src,
@@ -367,7 +371,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createBinop(mlir::Location loc, mlir::Value lhs,
cir::BinOpKind kind, mlir::Value rhs) {
- return create<cir::BinOp>(loc, lhs.getType(), kind, lhs, rhs);
+ return cir::BinOp::create(*this, loc, lhs.getType(), kind, lhs, rhs);
}
mlir::Value createLowBitsSet(mlir::Location loc, unsigned size,
@@ -389,8 +393,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value trueValue, mlir::Value falseValue) {
assert(trueValue.getType() == falseValue.getType() &&
"trueValue and falseValue should have the same type");
- return create<cir::SelectOp>(loc, trueValue.getType(), condition, trueValue,
- falseValue);
+ return cir::SelectOp::create(*this, loc, trueValue.getType(), condition,
+ trueValue, falseValue);
}
mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs,
@@ -405,8 +409,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
OverflowBehavior ob = OverflowBehavior::None) {
- auto op =
- create<cir::BinOp>(loc, lhs.getType(), cir::BinOpKind::Mul, lhs, rhs);
+ auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Mul,
+ lhs, rhs);
op.setNoUnsignedWrap(
llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
op.setNoSignedWrap(
@@ -424,8 +428,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
OverflowBehavior ob = OverflowBehavior::Saturated) {
- auto op =
- create<cir::BinOp>(loc, lhs.getType(), cir::BinOpKind::Sub, lhs, rhs);
+ auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Sub,
+ lhs, rhs);
op.setNoUnsignedWrap(
llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
op.setNoSignedWrap(
@@ -446,8 +450,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
OverflowBehavior ob = OverflowBehavior::None) {
- auto op =
- create<cir::BinOp>(loc, lhs.getType(), cir::BinOpKind::Add, lhs, rhs);
+ auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Add,
+ lhs, rhs);
op.setNoUnsignedWrap(
llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
op.setNoSignedWrap(
@@ -468,7 +472,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind,
mlir::Value lhs, mlir::Value rhs) {
- return create<cir::CmpOp>(loc, getBoolTy(), kind, lhs, rhs);
+ return cir::CmpOp::create(*this, loc, getBoolTy(), kind, lhs, rhs);
}
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand) {
@@ -477,7 +481,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
bool isShiftLeft) {
- return create<cir::ShiftOp>(loc, lhs.getType(), lhs, rhs, isShiftLeft);
+ return cir::ShiftOp::create(*this, loc, lhs.getType(), lhs, rhs,
+ isShiftLeft);
}
mlir::Value createShift(mlir::Location loc, mlir::Value lhs,
@@ -555,12 +560,12 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
/// Create a loop condition.
cir::ConditionOp createCondition(mlir::Value condition) {
- return create<cir::ConditionOp>(condition.getLoc(), condition);
+ return cir::ConditionOp::create(*this, condition.getLoc(), condition);
}
/// Create a yield operation.
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value = {}) {
- return create<cir::YieldOp>(loc, value);
+ return cir::YieldOp::create(*this, loc, value);
}
};
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 78f4ab6cae6f5..d5cb6d416bc9b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -291,7 +291,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
// Creates constant nullptr for pointer type ty.
cir::ConstantOp getNullPtr(mlir::Type ty, mlir::Location loc) {
assert(!cir::MissingFeatures::targetCodeGenInfoGetNullPointer());
- return create<cir::ConstantOp>(loc, getConstPtrAttr(ty, 0));
+ return cir::ConstantOp::create(*this, loc, getConstPtrAttr(ty, 0));
}
mlir::Value createNeg(mlir::Value value) {
@@ -300,7 +300,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
// Source is a unsigned integer: first cast it to signed.
if (intTy.isUnsigned())
value = createIntCast(value, getSIntNTy(intTy.getWidth()));
- return create<cir::UnaryOp>(value.getLoc(), value.getType(),
+ return cir::UnaryOp::create(*this, value.getLoc(), value.getType(),
cir::UnaryOpKind::Minus, value);
}
@@ -312,8 +312,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType) {
assert(!cir::MissingFeatures::fpConstraints());
- return create<cir::CastOp>(v.getLoc(), destType, cir::CastKind::floating,
- v);
+ return cir::CastOp::create(*this, v.getLoc(), destType,
+ cir::CastKind::floating, v);
}
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
@@ -321,7 +321,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
assert(!cir::MissingFeatures::fpConstraints());
assert(!cir::MissingFeatures::fastMathFlags());
- return create<cir::BinOp>(loc, cir::BinOpKind::Sub, lhs, rhs);
+ return cir::BinOp::create(*this, loc, cir::BinOpKind::Sub, lhs, rhs);
}
mlir::Value createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
@@ -329,21 +329,21 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
assert(!cir::MissingFeatures::fpConstraints());
assert(!cir::MissingFeatures::fastMathFlags());
- return create<cir::BinOp>(loc, cir::BinOpKind::Add, lhs, rhs);
+ return cir::BinOp::create(*this, loc, cir::BinOpKind::Add, lhs, rhs);
}
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
assert(!cir::MissingFeatures::metaDataNode());
assert(!cir::MissingFeatures::fpConstraints());
assert(!cir::MissingFeatures::fastMathFlags());
- return create<cir::BinOp>(loc, cir::BinOpKind::Mul, lhs, rhs);
+ return cir::BinOp::create(*this, loc, cir::BinOpKind::Mul, lhs, rhs);
}
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
assert(!cir::MissingFeatures::metaDataNode());
assert(!cir::MissingFeatures::fpConstraints());
assert(!cir::MissingFeatures::fastMathFlags());
- return create<cir::BinOp>(loc, cir::BinOpKind::Div, lhs, rhs);
+ return cir::BinOp::create(*this, loc, cir::BinOpKind::Div, lhs, rhs);
}
Address createBaseClassAddr(mlir::Location loc, Address addr,
@@ -353,8 +353,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
return addr;
auto ptrTy = getPointerTo(destType);
- auto baseAddr = create<cir::BaseClassAddrOp>(
- loc, ptrTy, addr.getPointer(), mlir::APInt(64, offset), assumeNotNull);
+ auto baseAddr =
+ cir::BaseClassAddrOp::create(*this, loc, ptrTy, addr.getPointer(),
+ mlir::APInt(64, offset), assumeNotNull);
return Address(baseAddr, destType, addr.getAlignment());
}
@@ -393,8 +394,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
mlir::Value createComplexRealPtr(mlir::Location loc, mlir::Value value) {
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
- return create<cir::ComplexRealPtrOp>(
- loc, getPointerTo(srcComplexTy.getElementType()), value);
+ return cir::ComplexRealPtrOp::create(
+ *this, loc, getPointerTo(srcComplexTy.getElementType()), value);
}
Address createComplexRealPtr(mlir::Location loc, Address addr) {
@@ -408,8 +409,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
mlir::Value createComplexImagPtr(mlir::Location loc, mlir::Value value) {
auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
- return create<cir::ComplexImagPtrOp>(
- loc, getPointerTo(srcComplexTy.getElementType()), value);
+ return cir::ComplexImagPtrOp::create(
+ *this, loc, getPointerTo(srcComplexTy.getElementType()), value);
}
Address createComplexImagPtr(mlir::Location loc, Address addr) {
@@ -467,9 +468,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
useVolatile ? cir::IntType::get(storageType.getContext(),
info.volatileStorageSize, info.isSigned)
: storageType;
- return create<cir::SetBitfieldOp>(
- loc, resultType, dstAddr.getPointer(), storageType, src, info.name,
- info.size, offset, info.isSigned, isLvalueVolatile,
+ return cir::SetBitfieldOp::create(
+ *this, loc, resultType, dstAddr.getPointer(), storageType, src,
+ info.name, info.size, offset, info.isSigned, isLvalueVolatile,
dstAddr.getAlignment().getAsAlign().value());
}
@@ -485,7 +486,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
useVolatile ? cir::IntType::get(storageType.getContext(),
info.volatileStorageSize, info.isSigned)
: storageType;
- return create<cir::GetBitfieldOp>(loc, resultType, addr.getPointer(),
+ return cir::GetBitfieldOp::create(*this, loc, resultType, addr.getPointer(),
storageType, info.name, info.size, offset,
info.isSigned, isLvalueVolatile,
addr.getAlignment().getAsAlign().value());
More information about the cfe-commits
mailing list