[flang-commits] [flang] [mlir] [mlir][func] Refactor FuncToLLVM discardable attributes algorithm (PR #188232)
Mehdi Amini via flang-commits
flang-commits at lists.llvm.org
Thu Mar 26 03:23:37 PDT 2026
================
@@ -59,19 +61,82 @@ static bool shouldUseBarePtrCallConv(Operation *op,
typeConverter->getOptions().useBarePtrCallConv;
}
+static bool isDiscardableAttr(StringRef name) {
+ return name == linkageAttrName || name == varargsAttrName ||
+ name == LLVM::LLVMDialect::getReadnoneAttrName();
+}
+
/// Only retain those attributes that are not constructed by
/// `LLVMFuncOp::build`.
static void filterFuncAttributes(FunctionOpInterface func,
SmallVectorImpl<NamedAttribute> &result) {
for (const NamedAttribute &attr : func->getDiscardableAttrs()) {
- if (attr.getName() == linkageAttrName ||
- attr.getName() == varargsAttrName ||
- attr.getName() == LLVM::LLVMDialect::getReadnoneAttrName())
+ if (isDiscardableAttr(attr.getName().strref()))
continue;
result.push_back(attr);
}
}
+/// Add custom lowered funcOp to llvm.func attributes here.
+struct LoweredFuncAttrs {
+ LLVM::LLVMFuncOp::Properties properties;
+ NamedAttrList discardableAttrs;
+};
+
+/// Lower discardable function attributes on `func.func` to attributes expected
+/// by `llvm.func`.static FailureOr<LoweredFuncAttrs>
+static FailureOr<LoweredFuncAttrs>
+lowerFuncAttributes(FunctionOpInterface func) {
+ MLIRContext *ctx = func->getContext();
+ LoweredFuncAttrs lowered;
+
+ llvm::SmallDenseSet<StringRef> odsAttrNames(
+ LLVM::LLVMFuncOp::getAttributeNames().begin(),
+ LLVM::LLVMFuncOp::getAttributeNames().end());
+
+ NamedAttrList inherentAttrs;
+
+ for (const NamedAttribute &attr : func->getDiscardableAttrs()) {
+ StringRef attrName = attr.getName().strref();
+
+ if (odsAttrNames.contains(attrName)) {
+ LDBG() << "LLVM specific attributes: " << attrName
+ << "should use llvm.* prefix, discarding it";
+ continue;
+ }
+
+ StringRef inherent = attrName;
+ if (inherent.consume_front("llvm.") && odsAttrNames.contains(inherent)) {
+ inherentAttrs.set(inherent, attr.getValue()); // collect inherent attrs
+ } else {
+ lowered.discardableAttrs.push_back(attr);
+ }
----------------
joker-eph wrote:
```suggestion
if (inherent.consume_front("llvm.") && odsAttrNames.contains(inherent))
inherentAttrs.set(inherent, attr.getValue()); // collect inherent attrs
else
lowered.discardableAttrs.push_back(attr);
```
Nit: no trivial braces
https://github.com/llvm/llvm-project/pull/188232
More information about the flang-commits
mailing list