[clang] [mlir] [CIR] Implement lowering for 'no-builtins' attributes (PR #178899)

Tobias Gysi via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 30 08:11:54 PST 2026


================
@@ -2683,15 +2691,55 @@ static constexpr std::array kExplicitLLVMFuncOpAttributes{
     StringLiteral("willreturn"),
 };
 
+// List of LLVM IR attributes that are handled by prefix to map onto an MLIR
+// LLVMFuncOp.
+static constexpr std::array kExplicitLLVMFuncOpAttributePrefixes{
+    StringLiteral("no-builtin-"),
+};
+
+template <typename OpTy>
+static void convertNoBuiltinAttrs(MLIRContext *ctx,
+                                  const llvm::AttributeSet &attrs,
+                                  OpTy target) {
+  // 'no-builtins' is the complete collection, and overrides all the rest.
+  if (attrs.hasAttribute("no-builtins")) {
+    target.setNobuiltinsAttr(mlir::ArrayAttr::get(ctx, {}));
+    return;
+  }
+
+  llvm::SmallVector<mlir::Attribute> nbAttrs;
+  for (llvm::Attribute attr : attrs) {
+    // Attributes that are part of llvm directly (that is, have an AttributeKind
+    // in the enum) shouldn't be checked.
+    if (attr.hasKindAsEnum())
+      continue;
+
+    StringRef val = attr.getKindAsString();
+
+    if (val.starts_with("no-builtin-")) {
+      StringRef str = val.drop_front(sizeof("no-builtin-") - 1);
+
+      if (nbAttrs.end() == llvm::find_if(nbAttrs, [str](Attribute a) {
+            return mlir::cast<StringAttr>(a).getValue() == str;
+          }))
+        nbAttrs.push_back(mlir::StringAttr::get(
+            ctx, val.drop_front(sizeof("no-builtin-") - 1)));
+    }
+  }
+
+  if (!nbAttrs.empty())
+    target.setNobuiltinsAttr(mlir::ArrayAttr::get(ctx, nbAttrs));
----------------
gysit wrote:

```suggestion
    target.setNobuiltinsAttr(ArrayAttr::get(ctx, nbAttrs));
```
nit: the mlir namespace is probably not needed here and above?

https://github.com/llvm/llvm-project/pull/178899


More information about the cfe-commits mailing list