[llvm] [NVPTX] Add Custom ABI (abi_preserve) support via function attributes (PR #227499)

Durgadoss R via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 30 04:14:36 PDT 2026


================
@@ -295,6 +296,59 @@ bool llvm::hasBlocksAreClusters(const Function &F) {
   return F.hasFnAttribute(NVVMAttr::BlocksAreClusters);
 }
 
+// PTX register-preservation ("custom ABI") directives. Each entry maps an NVVM
+// function attribute to the prototype directive it selects. This table is the
+// single point of extension for further preservation directives.
+static constexpr auto PreserveRegABIDirectives = std::array{
+    std::pair{NVVMAttr::PreserveNData, StringLiteral(".abi_preserve")},
+    std::pair{NVVMAttr::PreserveNControl,
+              StringLiteral(".abi_preserve_control")},
+};
+
+// Look the attribute up on the callsite only.
+static std::optional<unsigned> getFnAttrParsedInt(const CallBase &CB,
+                                                  StringRef Attr) {
+  const Attribute A = CB.getAttributes().getFnAttr(Attr);
+  if (!A.isValid())
+    return std::nullopt;
+
+  unsigned Value;
+  if (A.getValueAsString().getAsInteger(10, Value)) {
+    CB.getContext().emitError("can't parse integer attribute " +
+                              A.getValueAsString() + " in " + Attr);
+    return std::nullopt;
+  }
+  return Value;
+}
+
+static std::string formatABIPreserveDirectives(
+    bool Multiline, function_ref<std::optional<unsigned>(StringRef)> GetValue) {
+  std::string PrototypeAttributes;
+  for (const auto &[Attr, Directive] : PreserveRegABIDirectives) {
+    const std::optional<unsigned> Count = GetValue(Attr);
+    if (!Count)
+      continue;
+
+    if (!PrototypeAttributes.empty())
+      PrototypeAttributes += Multiline ? '\n' : ' ';
----------------
durga4github wrote:

can we initialize the multiline separator outside the loop...?

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


More information about the llvm-commits mailing list