[Mlir-commits] [llvm] [mlir] [MLIR] Add passthrough attribute to mlir.global (PR #154706)
Vadim Curcă
llvmlistbot at llvm.org
Fri Aug 22 06:45:16 PDT 2025
================
@@ -1081,6 +1081,89 @@ static void addRuntimePreemptionSpecifier(bool dsoLocalRequested,
gv->setDSOLocal(true);
}
+/// Attempts to add an attribute identified by `key`, optionally with the given
+/// `value`, using `addAttribute` function. Reports errors at `loc` if any. If
+/// the attribute has a kind known to LLVM IR, create the attribute of this
+/// kind, otherwise keep it as a string attribute. Performs additional checks
+/// for attributes known to have or not have a value in order to avoid
+/// assertions inside LLVM upon construction. Expects `addAttribute` callable to
+/// forward its arguments to a method that adds an attribute to an object.
+template <typename AddAttributeFn>
+static LogicalResult checkedAddAttribute(Location loc, llvm::LLVMContext &ctx,
+ AddAttributeFn &&addAttribute,
+ StringRef key,
+ StringRef value = StringRef()) {
+ auto kind = llvm::Attribute::getAttrKindFromName(key);
+ if (kind == llvm::Attribute::None) {
+ addAttribute(key, value);
+ return success();
+ }
+
+ if (llvm::Attribute::isIntAttrKind(kind)) {
+ if (value.empty())
+ return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
+
+ int64_t result;
+ if (!value.getAsInteger(/*Radix=*/0, result))
+ addAttribute(llvm::Attribute::get(ctx, kind, result));
+ else
+ addAttribute(key, value);
+ return success();
+ }
+
+ if (!value.empty())
+ return emitError(loc) << "LLVM attribute '" << key
+ << "' does not expect a value, found '" << value
+ << "'";
+
+ addAttribute(kind);
+ return success();
+}
+
+/// Adds the attributes listed in the given array attribute using `addAttribute`
+/// callable. Reports error to `loc` if any and returns immediately. Expects
+/// `attributes` to be an array attribute containing either string attributes,
+/// treated as value-less LLVM attributes, or array attributes containing two
+/// string attributes, with the first string being the name of the corresponding
+/// LLVM attribute and the second string beings its value. Note that even
+/// integer attributes are expected to have their values expressed as strings.
+/// Expects `addAttribute` callable to forward its arguments to a method that
+/// adds an attribute to an object.
+template <typename AddAttributeFn>
+static LogicalResult
+forwardPassthroughAttributes(Location loc, llvm::LLVMContext &ctx,
----------------
VadimCurca wrote:
Yes, thank you for the suggestion! I still ended up making some changes to the LLVM interface to allow adding attributes without converting from `llvm::AttrBuilder` to `llvm::AttributeSet` and back multiple times.
https://github.com/llvm/llvm-project/pull/154706
More information about the Mlir-commits
mailing list