[clang] [CIR] Let llvmOp lowering forward properties to the LLVM op (PR #240540)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 00:29:53 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/240540
>From 17fd0873bca7ec97a806f9060fa4b75bde544bdc Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Mon, 9 Nov 2026 20:55:02 +0000
Subject: [PATCH] [CIR] Let llvmOp lowering forward properties to the LLVM op
The lowering patterns generated from `llvmOp` build the LLVM operation with
default properties, so a CIR op whose flags have an LLVM counterpart had to
be lowered by hand. `llvmOpProps` lists the CIR attributes and properties to
forward; the emitter derives the CIR getter and the LLVM setter from the
names, and a renamed LLVM property is given as a string, as in
`(ins "is_zero_poison":$poison_zero)`. Values go through the
convertToLLVMProperty overload for their C++ type. The emitter rejects
entries that are not attributes or properties of the op.
cir.add, cir.sub and cir.mul (overflow flags), the floating-point vector
reductions (fastmath flags), cir.clz, cir.ctz, cir.abs and cir.is_fp_class
switch to it, and cir.popcount, cir.vec.extract and cir.vec.insert use plain
`llvmOp`. Their hand-written patterns and lowerFPVectorReduction go away.
Assisted-by: Claude Code (Claude Opus 5.5).
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 69 ++++++--
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 165 ++----------------
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.h | 12 ++
clang/utils/TableGen/CIRLoweringEmitter.cpp | 81 +++++++--
4 files changed, 156 insertions(+), 171 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index f75601738e2c0c..b9e0b602097d25 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -71,6 +71,18 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
// }
// }
//
+// The LLVM operation is built with default properties. `llvmOpProps` lists
+// CIR attributes and properties whose values are forwarded to it. Each value
+// is converted by the `convertToLLVMProperty` overload for its C++ type and
+// stored into the LLVM property of the same name, or of the name given as a
+// string when the two differ:
+//
+// let llvmOp = "AddOp";
+// let llvmOpProps = (ins $overflow_flags);
+//
+// let llvmOp = "CountLeadingZerosOp";
+// let llvmOpProps = (ins "is_zero_poison":$poison_zero);
+//
// If you want fully customized LLVM IR lowering logic, simply exclude the
// `llvmOp` field from your CIR operation definition.
//
@@ -85,6 +97,7 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
// exception-behavior metadata argument.
class LLVMLoweringInfo {
string llvmOp = "";
+ dag llvmOpProps = (ins);
string constrainedLLVMIntrinsic = "";
bit constrainedLLVMIntrinsicHasRoundingMode = true;
}
@@ -2837,8 +2850,8 @@ class CIR_BinaryOp<string mnemonic, Type type, list<Trait> traits = [],
}];
}
-class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
- list<Trait> traits = []>
+class CIR_BinaryOpWithOverflowFlags<string mnemonic, string llvmOpName,
+ Type type, list<Trait> traits = []>
: CIR_BinaryOp<mnemonic, type, traits>
{
let append traits = [Pure];
@@ -2850,6 +2863,9 @@ class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
let assemblyFormat = [{
($overflow_flags^)? $lhs `,` $rhs `:` type($lhs) attr-dict
}];
+
+ let llvmOp = llvmOpName;
+ let llvmOpProps = (ins $overflow_flags);
}
//===----------------------------------------------------------------------===//
@@ -2857,7 +2873,8 @@ class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
//===----------------------------------------------------------------------===//
def CIR_AddOp
- : CIR_BinaryOpWithOverflowFlags<"add", CIR_AnyIntOrVecOrMatrixOfIntType> {
+ : CIR_BinaryOpWithOverflowFlags<"add", "AddOp",
+ CIR_AnyIntOrVecOrMatrixOfIntType> {
let summary = "Integer addition";
let description = [{
The `cir.add` operation performs addition on integer operands. Both
@@ -2886,7 +2903,8 @@ def CIR_AddOp
//===----------------------------------------------------------------------===//
def CIR_SubOp
- : CIR_BinaryOpWithOverflowFlags<"sub", CIR_AnyIntOrVecOrMatrixOfIntType> {
+ : CIR_BinaryOpWithOverflowFlags<"sub", "SubOp",
+ CIR_AnyIntOrVecOrMatrixOfIntType> {
let summary = "Integer subtraction";
let description = [{
The `cir.sub` operation performs subtraction on integer operands. Both
@@ -2915,7 +2933,8 @@ def CIR_SubOp
//===----------------------------------------------------------------------===//
def CIR_MulOp
- : CIR_BinaryOpWithOverflowFlags<"mul", CIR_AnyIntOrVecOrMatrixOfIntType> {
+ : CIR_BinaryOpWithOverflowFlags<"mul", "MulOp",
+ CIR_AnyIntOrVecOrMatrixOfIntType> {
let summary = "Integer multiplication";
let description = [{
The `cir.mul` operation performs multiplication on integer operands. Both
@@ -6470,6 +6489,8 @@ def CIR_VecInsertOp : CIR_Op<"vec.insert", [
$value `,` $vec `[` $index `:` type($index) `]` attr-dict `:`
qualified(type($vec))
}];
+
+ let llvmOp = "InsertElementOp";
}
//===----------------------------------------------------------------------===//
@@ -6501,6 +6522,8 @@ def CIR_VecExtractOp : CIR_Op<"vec.extract", [
}];
let hasFolder = 1;
+
+ let llvmOp = "ExtractElementOp";
}
//===----------------------------------------------------------------------===//
@@ -6559,9 +6582,9 @@ def CIR_VecReduceXorOp
: CIR_VecReduceOp<"xor", CIR_VectorOfIntOrBoolType,
"vector_reduce_xor">;
-// Floating-point reductions use custom lowering to translate CIR fast-math
-// flags to the corresponding LLVM dialect attribute.
-class CIR_VecReduceFPOp<string mnemonic>
+// Floating-point reductions carry fast-math flags, forwarded to the LLVM op
+// through `llvmOpProps`.
+class CIR_VecReduceFPOp<string mnemonic, string llvmOpName>
: CIR_Op<"vec.reduce." # mnemonic, [
Pure,
TypesMatchWith<"type of 'result' matches element type of 'input'",
@@ -6579,12 +6602,15 @@ class CIR_VecReduceFPOp<string mnemonic>
`(` $input `)` `:` functional-type(operands, results)
prop-dict attr-dict
}];
+
+ let llvmOp = llvmOpName;
+ let llvmOpProps = (ins $fastmath_flags);
}
-def CIR_VecReduceFMaxOp : CIR_VecReduceFPOp<"fmax">;
-def CIR_VecReduceFMinOp : CIR_VecReduceFPOp<"fmin">;
+def CIR_VecReduceFMaxOp : CIR_VecReduceFPOp<"fmax", "vector_reduce_fmax">;
+def CIR_VecReduceFMinOp : CIR_VecReduceFPOp<"fmin", "vector_reduce_fmin">;
-class CIR_VecReduceAccFPOp<string mnemonic>
+class CIR_VecReduceAccFPOp<string mnemonic, string llvmOpName>
: CIR_Op<"vec.reduce." # mnemonic, [
Pure,
TypesMatchWith<"type of 'accumulator' matches element type of 'input'",
@@ -6606,10 +6632,13 @@ class CIR_VecReduceAccFPOp<string mnemonic>
`(` $accumulator `,` $input `)` `:` functional-type(operands, results)
prop-dict attr-dict
}];
+
+ let llvmOp = llvmOpName;
+ let llvmOpProps = (ins $fastmath_flags);
}
-def CIR_VecReduceFAddOp : CIR_VecReduceAccFPOp<"fadd">;
-def CIR_VecReduceFMulOp : CIR_VecReduceAccFPOp<"fmul">;
+def CIR_VecReduceFAddOp : CIR_VecReduceAccFPOp<"fadd", "vector_reduce_fadd">;
+def CIR_VecReduceFMulOp : CIR_VecReduceAccFPOp<"fmul", "vector_reduce_fmul">;
//===----------------------------------------------------------------------===//
// VecCmpOp
@@ -7777,6 +7806,8 @@ class CIR_BitZeroCountOpBase<string mnemonic, TypeConstraint operandTy>
$input (`poison_zero` $poison_zero^)?
`:` type($result) attr-dict
}];
+
+ let llvmOpProps = (ins "is_zero_poison":$poison_zero);
}
def CIR_BitClrsbOp : CIR_BitOpBase<"clrsb", CIR_SIntOfWidths<[32, 64]>> {
@@ -7832,6 +7863,8 @@ def CIR_BitClzOp : CIR_BitZeroCountOpBase<"clz",
%1 = cir.clz %0 poison_zero : !u32i
```
}];
+
+ let llvmOp = "CountLeadingZerosOp";
}
def CIR_BitCtzOp : CIR_BitZeroCountOpBase<"ctz",
@@ -7857,6 +7890,8 @@ def CIR_BitCtzOp : CIR_BitZeroCountOpBase<"ctz",
%1 = cir.ctz %0 poison_zero : !u32i
```
}];
+
+ let llvmOp = "CountTrailingZerosOp";
}
def CIR_BitFfsOp : CIR_BitOpBase<"ffs", CIR_SIntOfWidths<[32, 64]>> {
@@ -7916,6 +7951,8 @@ def CIR_BitPopcountOp : CIR_BitOpBase<"popcount", CIR_AnyUIntType > {
%1 = cir.popcount %0 : !u32i
```
}];
+
+ let llvmOp = "CtPopOp";
}
def CIR_BitReverseOp : CIR_BitOpBase<"bitreverse",
@@ -8156,6 +8193,9 @@ def CIR_IsFPClassOp : CIR_Op<"is_fp_class", [Pure]> {
let assemblyFormat = [{
$src `,` enum($flags) `:` functional-type($src, $result) attr-dict
}];
+
+ let llvmOp = "IsFPClass";
+ let llvmOpProps = (ins "bit":$flags);
}
//===----------------------------------------------------------------------===//
@@ -8722,6 +8762,9 @@ def CIR_AbsOp : CIR_Op<"abs", [Pure, SameOperandsAndResultType]> {
let results = (outs CIR_AnySIntOrVecOfSIntType:$result);
let assemblyFormat = "$src ( `min_is_poison` $min_is_poison^ )? `:` type($src) attr-dict";
+
+ let llvmOp = "AbsOp";
+ let llvmOpProps = (ins "is_int_min_poison":$min_is_poison);
}
def CIR_FloorOp : CIR_UnaryFPToFPBuiltinOp<"floor", "FFloorOp"> {
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 4a117865cab43e..1f23d51759ee31 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -727,6 +727,23 @@ mlir::LogicalResult lowerConstrainableFPOp(
fastmathFlags);
}
+mlir::LLVM::FastmathFlagsAttr
+convertToLLVMProperty(mlir::MLIRContext *ctx,
+ std::optional<cir::FastMathFlags> flags) {
+ return mlir::LLVM::FastmathFlagsAttr::get(
+ ctx, flags ? convertFastMathFlags(*flags) : mlir::LLVM::FastmathFlags{});
+}
+
+mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx,
+ cir::FPClassTest flags) {
+ return mlir::IntegerAttr::get(mlir::IntegerType::get(ctx, 32),
+ static_cast<uint32_t>(flags));
+}
+
+mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx, bool flag) {
+ return mlir::IntegerAttr::get(mlir::IntegerType::get(ctx, 1), flag);
+}
+
static mlir::LLVM::FastmathFlags
convertFastMathFlags(cir::FastMathFlags cirFlags) {
mlir::LLVM::FastmathFlags llvmFlags{};
@@ -1390,18 +1407,6 @@ struct ConvertCIRToLLVMPass
llvm::SmallVector<CollectedAnnotation> collectedAnnotations;
};
-mlir::LogicalResult CIRToLLVMIsFPClassOpLowering::matchAndRewrite(
- cir::IsFPClassOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- mlir::Value src = adaptor.getSrc();
- cir::FPClassTest flags = adaptor.getFlags();
- mlir::IntegerType retTy = rewriter.getI1Type();
-
- rewriter.replaceOpWithNewOp<mlir::LLVM::IsFPClass>(
- op, retTy, src, static_cast<uint32_t>(flags));
- return mlir::success();
-}
-
mlir::LogicalResult CIRToLLVMSignBitOpLowering::matchAndRewrite(
cir::SignBitOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
@@ -1855,26 +1860,6 @@ mlir::LogicalResult CIRToLLVMBitClrsbOpLowering::matchAndRewrite(
return mlir::LogicalResult::success();
}
-mlir::LogicalResult CIRToLLVMBitClzOpLowering::matchAndRewrite(
- cir::BitClzOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- auto resTy = getTypeConverter()->convertType(op.getType());
- auto llvmOp = mlir::LLVM::CountLeadingZerosOp::create(
- rewriter, op.getLoc(), resTy, adaptor.getInput(), op.getPoisonZero());
- rewriter.replaceOp(op, llvmOp);
- return mlir::LogicalResult::success();
-}
-
-mlir::LogicalResult CIRToLLVMBitCtzOpLowering::matchAndRewrite(
- cir::BitCtzOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- auto resTy = getTypeConverter()->convertType(op.getType());
- auto llvmOp = mlir::LLVM::CountTrailingZerosOp::create(
- rewriter, op.getLoc(), resTy, adaptor.getInput(), op.getPoisonZero());
- rewriter.replaceOp(op, llvmOp);
- return mlir::LogicalResult::success();
-}
-
mlir::LogicalResult CIRToLLVMBitFfsOpLowering::matchAndRewrite(
cir::BitFfsOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
@@ -1917,16 +1902,6 @@ mlir::LogicalResult CIRToLLVMBitParityOpLowering::matchAndRewrite(
return mlir::LogicalResult::success();
}
-mlir::LogicalResult CIRToLLVMBitPopcountOpLowering::matchAndRewrite(
- cir::BitPopcountOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- auto resTy = getTypeConverter()->convertType(op.getType());
- auto llvmOp = mlir::LLVM::CtPopOp::create(rewriter, op.getLoc(), resTy,
- adaptor.getInput());
- rewriter.replaceOp(op, llvmOp);
- return mlir::LogicalResult::success();
-}
-
mlir::LogicalResult CIRToLLVMBrCondOpLowering::matchAndRewrite(
cir::BrCondOp brOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
@@ -3109,17 +3084,6 @@ mlir::LogicalResult CIRToLLVMExpectOpLowering::matchAndRewrite(
return mlir::success();
}
-mlir::LogicalResult CIRToLLVMAbsOpLowering::matchAndRewrite(
- cir::AbsOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- mlir::Type resTy = typeConverter->convertType(op.getType());
- auto absOp = mlir::LLVM::AbsOp::create(rewriter, op.getLoc(), resTy,
- adaptor.getOperands()[0],
- adaptor.getMinIsPoison());
- rewriter.replaceOp(op, absOp);
- return mlir::success();
-}
-
/// Return true for discardable CIR markers handled explicitly below.
static bool isHandledDiscardableFuncAttr(mlir::NamedAttribute attr) {
return attr.getName() == CIRDialect::getNoReturnAttrName() ||
@@ -3928,8 +3892,8 @@ static bool isIntTypeUnsigned(mlir::Type type) {
// Binary Op Lowering
//===----------------------------------------------------------------------===//
-static mlir::LLVM::IntegerOverflowFlags
-convertOverflowFlags(cir::OverflowFlags flags) {
+mlir::LLVM::IntegerOverflowFlags
+convertToLLVMProperty(mlir::MLIRContext *, cir::OverflowFlags flags) {
mlir::LLVM::IntegerOverflowFlags result =
mlir::LLVM::IntegerOverflowFlags::none;
if (bitEnumContainsAny(flags, cir::OverflowFlags::NoSignedWrap))
@@ -3939,33 +3903,6 @@ convertOverflowFlags(cir::OverflowFlags flags) {
return result;
}
-mlir::LogicalResult CIRToLLVMAddOpLowering::matchAndRewrite(
- cir::AddOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- rewriter.replaceOpWithNewOp<mlir::LLVM::AddOp>(
- op, adaptor.getLhs(), adaptor.getRhs(),
- convertOverflowFlags(op.getOverflowFlags()));
- return mlir::success();
-}
-
-mlir::LogicalResult CIRToLLVMSubOpLowering::matchAndRewrite(
- cir::SubOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- rewriter.replaceOpWithNewOp<mlir::LLVM::SubOp>(
- op, adaptor.getLhs(), adaptor.getRhs(),
- convertOverflowFlags(op.getOverflowFlags()));
- return mlir::success();
-}
-
-mlir::LogicalResult CIRToLLVMMulOpLowering::matchAndRewrite(
- cir::MulOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- rewriter.replaceOpWithNewOp<mlir::LLVM::MulOp>(
- op, adaptor.getLhs(), adaptor.getRhs(),
- convertOverflowFlags(op.getOverflowFlags()));
- return mlir::success();
-}
-
/// Lower an integer op to its signed or unsigned LLVM counterpart.
template <typename UIntOp, typename SIntOp, typename CIROp>
static mlir::LogicalResult
@@ -5704,70 +5641,6 @@ mlir::LogicalResult CIRToLLVMVecCreateOpLowering::matchAndRewrite(
return mlir::success();
}
-mlir::LogicalResult CIRToLLVMVecExtractOpLowering::matchAndRewrite(
- cir::VecExtractOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- rewriter.replaceOpWithNewOp<mlir::LLVM::ExtractElementOp>(
- op, adaptor.getVec(), adaptor.getIndex());
- return mlir::success();
-}
-
-mlir::LogicalResult CIRToLLVMVecInsertOpLowering::matchAndRewrite(
- cir::VecInsertOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- rewriter.replaceOpWithNewOp<mlir::LLVM::InsertElementOp>(
- op, adaptor.getVec(), adaptor.getValue(), adaptor.getIndex());
- return mlir::success();
-}
-
-template <typename LLVMOp, typename CIROp>
-static mlir::LogicalResult
-lowerFPVectorReduction(CIROp op, typename CIROp::Adaptor adaptor,
- const mlir::TypeConverter &typeConverter,
- mlir::ConversionPatternRewriter &rewriter) {
- mlir::Type resultTy = typeConverter.convertType(op.getType());
- mlir::LLVM::FastmathFlags fastmathFlags{};
- if (std::optional<cir::FastMathFlags> fastmath = op.getFastmathFlags())
- fastmathFlags = convertFastMathFlags(*fastmath);
-
- typename LLVMOp::Properties properties =
- cir::getDefaultProperties<LLVMOp>(op.getContext());
- properties.setFastmathFlags(
- mlir::LLVM::FastmathFlagsAttr::get(op.getContext(), fastmathFlags));
- rewriter.replaceOpWithNewOp<LLVMOp>(op, mlir::TypeRange{resultTy},
- adaptor.getOperands(), properties);
-
- return mlir::success();
-}
-
-mlir::LogicalResult CIRToLLVMVecReduceFAddOpLowering::matchAndRewrite(
- cir::VecReduceFAddOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- return lowerFPVectorReduction<mlir::LLVM::vector_reduce_fadd>(
- op, adaptor, *getTypeConverter(), rewriter);
-}
-
-mlir::LogicalResult CIRToLLVMVecReduceFMulOpLowering::matchAndRewrite(
- cir::VecReduceFMulOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- return lowerFPVectorReduction<mlir::LLVM::vector_reduce_fmul>(
- op, adaptor, *getTypeConverter(), rewriter);
-}
-
-mlir::LogicalResult CIRToLLVMVecReduceFMaxOpLowering::matchAndRewrite(
- cir::VecReduceFMaxOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- return lowerFPVectorReduction<mlir::LLVM::vector_reduce_fmax>(
- op, adaptor, *getTypeConverter(), rewriter);
-}
-
-mlir::LogicalResult CIRToLLVMVecReduceFMinOpLowering::matchAndRewrite(
- cir::VecReduceFMinOp op, OpAdaptor adaptor,
- mlir::ConversionPatternRewriter &rewriter) const {
- return lowerFPVectorReduction<mlir::LLVM::vector_reduce_fmin>(
- op, adaptor, *getTypeConverter(), rewriter);
-}
-
mlir::LogicalResult CIRToLLVMVecCmpOpLowering::matchAndRewrite(
cir::VecCmpOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
index 692a424ccce5b4..b2eff91558352f 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
@@ -104,6 +104,18 @@ mlir::LogicalResult lowerConstrainableFPOp(
mlir::ConversionPatternRewriter &rewriter,
llvm::StringRef constrainedMnemonic, bool hasRoundingMode);
+// Convert the value of a CIR attribute or property to the LLVM property that
+// a generated lowering forwards it to (`llvmOpProps`). The overload is picked
+// by the C++ type of the CIR value.
+mlir::LLVM::IntegerOverflowFlags
+convertToLLVMProperty(mlir::MLIRContext *ctx, cir::OverflowFlags flags);
+mlir::LLVM::FastmathFlagsAttr
+convertToLLVMProperty(mlir::MLIRContext *ctx,
+ std::optional<cir::FastMathFlags> flags);
+mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx,
+ cir::FPClassTest flags);
+mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx, bool flag);
+
#define GET_LLVM_LOWERING_PATTERNS
#include "clang/CIR/Dialect/IR/CIRLowering.inc"
#undef GET_LLVM_LOWERING_PATTERNS
diff --git a/clang/utils/TableGen/CIRLoweringEmitter.cpp b/clang/utils/TableGen/CIRLoweringEmitter.cpp
index 285fc8d67b1483..136ce3a49f8994 100644
--- a/clang/utils/TableGen/CIRLoweringEmitter.cpp
+++ b/clang/utils/TableGen/CIRLoweringEmitter.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "TableGenBackends.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
@@ -40,6 +41,13 @@ struct CustomLoweringCtor {
std::vector<Param> Params;
};
+// A CIR attribute or property whose value the generated lowering converts
+// and stores into a property of the LLVM operation (`llvmOpProps`).
+struct ForwardedProp {
+ std::string CIRGetter;
+ std::string LLVMSetter;
+};
+
// Adapted from mlir/lib/TableGen/Operator.cpp
// Returns the C++ class name of the operation, which is the name of the
// operation with the dialect prefix removed and the first underscore removed.
@@ -88,6 +96,41 @@ std::string GetOpLLVMLoweringPatternName(llvm::StringRef OpName) {
Name += "Lowering";
return Name;
}
+std::vector<ForwardedProp> parseForwardedProps(const Record *OpRecord) {
+ const DagInit *Props = OpRecord->getValueAsDag("llvmOpProps");
+ const DagInit *Args = OpRecord->getValueAsDag("arguments");
+ std::vector<ForwardedProp> Result;
+ for (const auto &[Value, Name] : Props->getArgAndNames()) {
+ if (!Name)
+ PrintFatalError(OpRecord->getLoc(),
+ "llvmOpProps entries must name a CIR argument");
+ llvm::StringRef CIRName = Name->getValue();
+ std::optional<unsigned> ArgNo = Args->getArgNo(CIRName);
+ const auto *Arg = ArgNo ? dyn_cast<DefInit>(Args->getArg(*ArgNo)) : nullptr;
+ if (!Arg || !(Arg->getDef()->isSubClassOf("Attr") ||
+ Arg->getDef()->isSubClassOf("Property")))
+ PrintFatalError(OpRecord->getLoc(),
+ "llvmOpProps entry '" + CIRName +
+ "' is not an attribute or property of the op");
+
+ // The LLVM property has the same name unless the entry gives one.
+ llvm::StringRef LLVMName = CIRName;
+ if (const auto *Str = dyn_cast<StringInit>(Value))
+ LLVMName = Str->getValue();
+ else if (!isa<UnsetInit>(Value))
+ PrintFatalError(OpRecord->getLoc(),
+ "llvmOpProps entry '" + CIRName +
+ "' must map to an LLVM property name string");
+
+ Result.push_back(
+ {"get" + llvm::convertToCamelFromSnakeCase(CIRName,
+ /*capitalizeFirst=*/true),
+ "set" + llvm::convertToCamelFromSnakeCase(LLVMName,
+ /*capitalizeFirst=*/true)});
+ }
+ return Result;
+}
+
std::optional<CustomLoweringCtor> parseCustomLoweringCtor(const Record *R) {
if (!R)
return std::nullopt;
@@ -147,14 +190,12 @@ void GenerateABILoweringPattern(llvm::StringRef OpName,
CXXABILoweringPatterns.push_back(std::move(CodeBuffer));
}
-void GenerateLLVMLoweringPattern(llvm::StringRef OpName,
- llvm::StringRef PatternName, bool IsRecursive,
- llvm::StringRef ExtraDecl,
- const Record *CustomCtorRec,
- llvm::StringRef LLVMOp,
- llvm::StringRef ConstrainedLLVMIntrinsic,
- bool ConstrainedHasRoundingMode,
- bool HasFastMathFlags, bool HasZeroResult) {
+void GenerateLLVMLoweringPattern(
+ llvm::StringRef OpName, llvm::StringRef PatternName, bool IsRecursive,
+ llvm::StringRef ExtraDecl, const Record *CustomCtorRec,
+ llvm::StringRef LLVMOp, llvm::ArrayRef<ForwardedProp> ForwardedProps,
+ llvm::StringRef ConstrainedLLVMIntrinsic, bool ConstrainedHasRoundingMode,
+ bool HasFastMathFlags, bool HasZeroResult) {
std::optional<CustomLoweringCtor> CustomCtor =
parseCustomLoweringCtor(CustomCtorRec);
std::string CodeBuffer;
@@ -232,6 +273,14 @@ void GenerateLLVMLoweringPattern(llvm::StringRef OpName,
std::string Properties =
"cir::getDefaultProperties<mlir::LLVM::" + LLVMOp.str() +
">(op.getContext())";
+ if (!ForwardedProps.empty()) {
+ Code << " auto props = " << Properties << ";\n";
+ for (const ForwardedProp &Prop : ForwardedProps)
+ Code << " props." << Prop.LLVMSetter
+ << "(convertToLLVMProperty(op.getContext(), op." << Prop.CIRGetter
+ << "()));\n";
+ Properties = "props";
+ }
if (HasZeroResult) {
Code << " rewriter.replaceOpWithNewOp<mlir::LLVM::" << LLVMOp
<< ">(op, mlir::TypeRange{}, adaptor.getOperands(), " << Properties
@@ -283,6 +332,7 @@ void Generate(const Record *OpRecord) {
OpRecord->getValueAsString("extraLLVMLoweringPatternDecl");
llvm::StringRef LLVMOp = OpRecord->getValueAsString("llvmOp");
+ std::vector<ForwardedProp> ForwardedProps = parseForwardedProps(OpRecord);
llvm::StringRef ConstrainedLLVMIntrinsic =
OpRecord->getValueAsString("constrainedLLVMIntrinsic");
bool ConstrainedHasRoundingMode =
@@ -305,12 +355,19 @@ void Generate(const Record *OpRecord) {
"' has constrainedLLVMIntrinsic set but no llvmOp, "
"which is not supported");
+ if (!ForwardedProps.empty() &&
+ (LLVMOp.empty() || !ConstrainedLLVMIntrinsic.empty()))
+ PrintFatalError(OpRecord->getLoc(),
+ "op '" + OpName +
+ "' has llvmOpProps, which requires llvmOp and is "
+ "not supported with constrainedLLVMIntrinsic");
+
const DagInit *ResultsDag = OpRecord->getValueAsDag("results");
bool IsZeroResult = ResultsDag->getNumArgs() == 0;
- GenerateLLVMLoweringPattern(OpName, PatternName, IsRecursive, ExtraDecl,
- CustomCtor, LLVMOp, ConstrainedLLVMIntrinsic,
- ConstrainedHasRoundingMode, HasFastMathFlags,
- IsZeroResult);
+ GenerateLLVMLoweringPattern(
+ OpName, PatternName, IsRecursive, ExtraDecl, CustomCtor, LLVMOp,
+ ForwardedProps, ConstrainedLLVMIntrinsic, ConstrainedHasRoundingMode,
+ HasFastMathFlags, IsZeroResult);
// Only automatically register patterns that use the default constructor.
// Patterns with a custom constructor must be manually registered by the
// lowering pass.
More information about the cfe-commits
mailing list