From e3cae8508ecb37593c45b1ed28d66a99a62146cc Mon Sep 17 00:00:00 2001 From: Stefan Stipanovic Date: Tue, 25 Feb 2025 10:56:55 +0100 Subject: [PATCH 1/2] Argument attributes support --- example/ExampleDialect.td | 2 +- include/llvm-dialects/Dialect/Dialect.td | 12 +++++ include/llvm-dialects/TableGen/Traits.h | 1 + lib/TableGen/GenDialect.cpp | 7 ++- lib/TableGen/Traits.cpp | 38 ++++++++++++-- test/example/generated/ExampleDialect.cpp.inc | 52 ++++++++++++------- test/example/generated/ExampleDialect.h.inc | 2 +- 7 files changed, 88 insertions(+), 26 deletions(-) diff --git a/example/ExampleDialect.td b/example/ExampleDialect.td index 4c2e507..94f5a01 100644 --- a/example/ExampleDialect.td +++ b/example/ExampleDialect.td @@ -148,7 +148,7 @@ def Add32Op : ExampleOp<"add32", [Memory<[]>, NoUnwind, WillReturn]> { }]; } -def SizeOfOp : ExampleOp<"sizeof", [Memory<[]>, NoUnwind, WillReturn]> { +def SizeOfOp : ExampleOp<"sizeof", [Memory<[]>, NoUnwind, WillReturn, NoCapture>]> { let results = (outs I64:$result); let arguments = (ins type:$sizeof_type); diff --git a/include/llvm-dialects/Dialect/Dialect.td b/include/llvm-dialects/Dialect/Dialect.td index dcea24a..828b2f1 100644 --- a/include/llvm-dialects/Dialect/Dialect.td +++ b/include/llvm-dialects/Dialect/Dialect.td @@ -328,6 +328,10 @@ class LlvmEnumAttributeTrait : Trait { string llvmEnum = llvmEnum_; } +class LlvmEnumArgAttributeTrait : LlvmEnumAttributeTrait { + int idx; +} + def NoUnwind : LlvmEnumAttributeTrait<"NoUnwind">; def WillReturn : LlvmEnumAttributeTrait<"WillReturn">; def NoReturn : LlvmEnumAttributeTrait<"NoReturn">; @@ -346,6 +350,14 @@ def Hot : LlvmEnumAttributeTrait<"Hot">; def Convergent : LlvmEnumAttributeTrait<"Convergent">; def Speculatable : LlvmEnumAttributeTrait<"Speculatable">; +class ArgIndex { + int argNo = argNo_; +} + +class NoCapture : LlvmEnumArgAttributeTrait<"NoCapture"> { + let idx = argIdx.argNo; +} + /// Represent the LLVM `memory(...)` attribute as the OR (or union) of memory /// effects. An empty effects list means the operation does not access memory /// at all. If this trait is missing, it means that there is no bound on the diff --git a/include/llvm-dialects/TableGen/Traits.h b/include/llvm-dialects/TableGen/Traits.h index 0a4eaad..0dbcadb 100644 --- a/include/llvm-dialects/TableGen/Traits.h +++ b/include/llvm-dialects/TableGen/Traits.h @@ -40,6 +40,7 @@ class Trait { enum class Kind : uint8_t { LlvmAttributeTrait_First, LlvmEnumAttributeTrait = LlvmAttributeTrait_First, + LlvmEnumArgAttributeTrait, LlvmMemoryAttributeTrait, LlvmAttributeTrait_Last = LlvmMemoryAttributeTrait, }; diff --git a/lib/TableGen/GenDialect.cpp b/lib/TableGen/GenDialect.cpp index 2d6a465..941c01e 100644 --- a/lib/TableGen/GenDialect.cpp +++ b/lib/TableGen/GenDialect.cpp @@ -343,10 +343,13 @@ void llvm_dialects::genDialectDefs(raw_ostream &out, RecordKeeperTy &records) { if (!dialect->attribute_lists_empty()) { FmtContextScope scope{fmt}; fmt.addSubst("attrBuilder", "attrBuilder"); + fmt.addSubst("argAttrList", "argAttrList"); for (const auto &enumeratedTraits : enumerate(dialect->attribute_lists())) { out << tgfmt("{\n ::llvm::AttrBuilder $attrBuilder{context};\n", &fmt); + out << tgfmt(" ::llvm::AttributeList $argAttrList;\n", &fmt); + SmallVector argTraits; for (const Trait *trait : enumeratedTraits.value()) { if (auto *llvmAttribute = dyn_cast(trait)) { llvmAttribute->addAttribute(out, fmt); @@ -355,8 +358,8 @@ void llvm_dialects::genDialectDefs(raw_ostream &out, RecordKeeperTy &records) { } } - out << tgfmt("m_attributeLists[$0] = ::llvm::AttributeList::get(context, " - "::llvm::AttributeList::FunctionIndex, $attrBuilder);\n}\n", + out << tgfmt("m_attributeLists[$0] = " + "$argAttrList.addFnAttributes(context, $attrBuilder);\n}\n", &fmt, enumeratedTraits.index()); } } diff --git a/lib/TableGen/Traits.cpp b/lib/TableGen/Traits.cpp index 7f5a18a..fcd42a4 100644 --- a/lib/TableGen/Traits.cpp +++ b/lib/TableGen/Traits.cpp @@ -36,7 +36,7 @@ static cl::opt NoMemoryEffects( class LlvmEnumAttributeTrait : public LlvmAttributeTrait { public: - LlvmEnumAttributeTrait() : LlvmAttributeTrait(Kind::LlvmEnumAttributeTrait) {} + LlvmEnumAttributeTrait(Kind kind) : LlvmAttributeTrait(kind) {} void init(GenDialectsContext *context, RecordTy *record) override; @@ -52,6 +52,24 @@ class LlvmEnumAttributeTrait : public LlvmAttributeTrait { std::string m_llvmEnum; }; +class LlvmEnumArgAttributeTrait : public LlvmEnumAttributeTrait { +public: + LlvmEnumArgAttributeTrait() : LlvmEnumAttributeTrait(Kind::LlvmEnumArgAttributeTrait) {} + + void init(GenDialectsContext *context, RecordTy *record) override; + + void addAttribute(llvm::raw_ostream &out, FmtContext &fmt) const override; + + int getIdx() const { return m_idx; } + + static bool classof(const Trait *t) { + return t->getKind() == Kind::LlvmEnumArgAttributeTrait; + } + +private: + int m_idx; +}; + class LlvmMemoryAttributeTrait : public LlvmAttributeTrait { public: LlvmMemoryAttributeTrait() @@ -85,8 +103,10 @@ bool llvm_dialects::noMemoryEffects() { std::unique_ptr Trait::fromRecord(GenDialectsContext *context, RecordTy *traitRec) { std::unique_ptr result; - if (traitRec->isSubClassOf("LlvmEnumAttributeTrait")) { - result = std::make_unique(); + if (traitRec->isSubClassOf("LlvmEnumArgAttributeTrait")) { + result = std::make_unique(); + } else if (traitRec->isSubClassOf("LlvmEnumAttributeTrait")) { + result = std::make_unique(Kind::LlvmEnumAttributeTrait); } else if (traitRec->isSubClassOf("Memory")) { result = std::make_unique(); } else { @@ -108,12 +128,24 @@ void LlvmEnumAttributeTrait::init(GenDialectsContext *context, m_llvmEnum = record->getValueAsString("llvmEnum"); } +void LlvmEnumArgAttributeTrait::init(GenDialectsContext *context, + RecordTy *record) { + LlvmEnumAttributeTrait::init(context, record); + m_idx = record->getValueAsInt("idx"); +} + void LlvmEnumAttributeTrait::addAttribute(raw_ostream &out, FmtContext &fmt) const { out << tgfmt("$attrBuilder.addAttribute(::llvm::Attribute::$0);\n", &fmt, getLlvmEnum()); } +void LlvmEnumArgAttributeTrait::addAttribute(raw_ostream &out, + FmtContext &fmt) const { + out << tgfmt("$argAttrList = $argAttrList.addParamAttribute(context, $0, ::llvm::Attribute::$1);\n", + &fmt, getIdx(), getLlvmEnum()); +} + void LlvmMemoryAttributeTrait::init(GenDialectsContext *context, RecordTy *record) { LlvmAttributeTrait::init(context, record); diff --git a/test/example/generated/ExampleDialect.cpp.inc b/test/example/generated/ExampleDialect.cpp.inc index 06f313a..449927d 100644 --- a/test/example/generated/ExampleDialect.cpp.inc +++ b/test/example/generated/ExampleDialect.cpp.inc @@ -185,35 +185,49 @@ namespace xd::cpp { ExampleDialect::ExampleDialect(::llvm::LLVMContext& context) : DialectImpl(context) { { ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; attrBuilder.addAttribute(::llvm::Attribute::NoUnwind); attrBuilder.addAttribute(::llvm::Attribute::WillReturn); attrBuilder.addMemoryAttr(::llvm::MemoryEffects::none()); -m_attributeLists[0] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder); +m_attributeLists[0] = argAttrList.addFnAttributes(context, attrBuilder); } { ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; +attrBuilder.addAttribute(::llvm::Attribute::NoUnwind); +attrBuilder.addAttribute(::llvm::Attribute::WillReturn); +attrBuilder.addMemoryAttr(::llvm::MemoryEffects::none()); +argAttrList = argAttrList.addParamAttribute(context, 0, ::llvm::Attribute::NoCapture); +m_attributeLists[1] = argAttrList.addFnAttributes(context, attrBuilder); +} +{ + ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; attrBuilder.addAttribute(::llvm::Attribute::NoUnwind); attrBuilder.addAttribute(::llvm::Attribute::WillReturn); attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::ModRefInfo::Ref)); -m_attributeLists[1] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder); +m_attributeLists[2] = argAttrList.addFnAttributes(context, attrBuilder); } { ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; attrBuilder.addAttribute(::llvm::Attribute::NoUnwind); attrBuilder.addAttribute(::llvm::Attribute::WillReturn); attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::MemoryEffects::Location::InaccessibleMem, ::llvm::ModRefInfo::Mod)); -m_attributeLists[2] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder); +m_attributeLists[3] = argAttrList.addFnAttributes(context, attrBuilder); } { ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; attrBuilder.addAttribute(::llvm::Attribute::NoUnwind); attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::MemoryEffects::Location::InaccessibleMem, ::llvm::ModRefInfo::ModRef)); -m_attributeLists[3] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder); +m_attributeLists[4] = argAttrList.addFnAttributes(context, attrBuilder); } { ::llvm::AttrBuilder attrBuilder{context}; + ::llvm::AttributeList argAttrList; attrBuilder.addAttribute(::llvm::Attribute::WillReturn); -m_attributeLists[4] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder); +m_attributeLists[5] = argAttrList.addFnAttributes(context, attrBuilder); } } @@ -1111,7 +1125,7 @@ source const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), { ::llvm::IntegerType::get(context, 1), }, false); @@ -1302,7 +1316,7 @@ index const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::IntegerType::get(context, 32), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -1386,7 +1400,7 @@ instName_0 const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::IntegerType::get(context, 32), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -1460,7 +1474,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::IntegerType::get(context, 32), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -1640,7 +1654,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(3); + = ExampleDialect::get(context).getAttributeList(4); std::string mangledName = ::llvm_dialects::getMangledName(s_name, {dataType}); @@ -1697,7 +1711,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(3); + = ExampleDialect::get(context).getAttributeList(4); std::string mangledName = ::llvm_dialects::getMangledName(s_name, {dataType}); @@ -1754,7 +1768,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(2); + = ExampleDialect::get(context).getAttributeList(3); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -1817,7 +1831,7 @@ data const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(0); + = ExampleDialect::get(context).getAttributeList(1); auto fnType = ::llvm::FunctionType::get(::llvm::IntegerType::get(context, 64), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -1891,7 +1905,7 @@ data const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(1); + = ExampleDialect::get(context).getAttributeList(2); std::string mangledName = ::llvm_dialects::getMangledName(s_name, {initial->getType()}); @@ -1983,7 +1997,7 @@ initial const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(1); + = ExampleDialect::get(context).getAttributeList(2); std::string mangledName = ::llvm_dialects::getMangledName(s_name, {initial->getType()}); @@ -2075,7 +2089,7 @@ initial const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(1); + = ExampleDialect::get(context).getAttributeList(2); std::string mangledName = ::llvm_dialects::getMangledName(s_name, {initial->getType()}); @@ -2167,7 +2181,7 @@ initial const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), { ::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0), }, false); @@ -2236,7 +2250,7 @@ initial const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(2); + = ExampleDialect::get(context).getAttributeList(3); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); @@ -2299,7 +2313,7 @@ data const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(2); + = ExampleDialect::get(context).getAttributeList(3); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true); auto fn = module.getOrInsertFunction(s_name, fnType, attrs); diff --git a/test/example/generated/ExampleDialect.h.inc b/test/example/generated/ExampleDialect.h.inc index 0553f53..15f677f 100644 --- a/test/example/generated/ExampleDialect.h.inc +++ b/test/example/generated/ExampleDialect.h.inc @@ -47,7 +47,7 @@ namespace xd::cpp { } private: - ::std::array<::llvm::AttributeList, 5> m_attributeLists; + ::std::array<::llvm::AttributeList, 6> m_attributeLists; }; class XdHandleType : public ::llvm::TargetExtType { From 4cab801b728d8fa07d9807d81a81b4e58e04251e Mon Sep 17 00:00:00 2001 From: Stefan Stipanovic Date: Tue, 25 Feb 2025 16:30:52 +0100 Subject: [PATCH 2/2] rebase and fix test --- test/example/generated/ExampleDialect.cpp.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/example/generated/ExampleDialect.cpp.inc b/test/example/generated/ExampleDialect.cpp.inc index 449927d..96dabdc 100644 --- a/test/example/generated/ExampleDialect.cpp.inc +++ b/test/example/generated/ExampleDialect.cpp.inc @@ -1552,7 +1552,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), { }, false); @@ -1603,7 +1603,7 @@ instName const ::llvm::AttributeList attrs - = ExampleDialect::get(context).getAttributeList(4); + = ExampleDialect::get(context).getAttributeList(5); auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), { }, false);