[llvm] [NVPTX] Diagnose an unsupported alias instead of aborting the compiler (PR #222651)
Ashutosh Kumar Singh via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 07:02:00 PDT 2026
https://github.com/Ashutosh0x updated https://github.com/llvm/llvm-project/pull/222651
>From eeb427428a06fdfae6390abc5283ade29cd2342d Mon Sep 17 00:00:00 2001
From: Ashutosh0x <161562995+Ashutosh0x at users.noreply.github.com>
Date: Thu, 10 Sep 2026 19:01:20 +0530
Subject: [PATCH] [NVPTX] Diagnose an unsupported alias instead of aborting the
compiler
emitAliasDeclaration() rejected four shapes of GlobalAlias with
report_fatal_error, so a module that is perfectly valid IR took the whole
compilation down with an abort, no source location, and a "PLEASE submit a bug
report" banner. A frontend that emits an alias for every export -- the Zig
compiler does -- hits this for any exported kernel.
These aliases genuinely cannot be lowered. PTX's .alias directive says "Both
fAlias and fAliasee are non-entry function symbols", that the aliasee "must be
defined in the same module", and that it "cannot have .weak linkage", so a
kernel alias in particular has no PTX spelling at all. The restriction is
right; only the way it was reported was wrong.
Report them with DiagnosticInfoUnsupported, matching how this file already
handles an unsupported blocksareclusters, and skip the .alias directive for an
alias whose declaration was skipped. llc now exits non-zero with a readable
error rather than aborting.
The reason strings are split out into getAliasUnsupportedReason() so the
declaration and the directive cannot disagree about what is lowerable.
Fixes #213504.
Assisted-by: Claude Code (Anthropic)
---
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp | 56 +++++++++++++++++++----
llvm/test/CodeGen/NVPTX/alias-errors.ll | 26 +++++++++--
2 files changed, 70 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index e2b55b3be4dc0..ae8a51e4ea7a1 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -67,6 +67,7 @@
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalValue.h"
@@ -1045,18 +1046,50 @@ std::string NVPTXAsmPrinter::getVirtualRegisterName(Register Reg) const {
return Name;
}
+/// PTX's .alias directive states that "Both fAlias and fAliasee are non-entry
+/// function symbols", that the aliasee "must be defined in the same module",
+/// and that it "cannot have .weak linkage" (PTX ISA, Kernel and Function
+/// Directives: .alias). An alias outside those bounds therefore has no PTX
+/// lowering at all. Returns why the alias cannot be lowered, or nullptr if it
+/// can be.
+static const char *getAliasUnsupportedReason(const GlobalAlias &GA) {
+ const Function *F = dyn_cast_or_null<Function>(GA.getAliaseeObject());
+ if (!F)
+ return "PTX can only alias a function";
+ if (F->isDeclaration())
+ return "PTX requires the aliasee to be defined in the same module";
+ if (isKernelFunction(*F))
+ return "PTX .alias requires non-entry functions, so a kernel cannot be "
+ "aliased";
+ if (GA.hasLinkOnceLinkage() || GA.hasWeakLinkage() ||
+ GA.hasAvailableExternallyLinkage() || GA.hasCommonLinkage())
+ return "PTX forbids a .weak aliasee";
+ return nullptr;
+}
+
+/// Report an alias we cannot lower as a diagnostic rather than aborting. A
+/// frontend that emits an alias for every export (as the Zig compiler does)
+/// would otherwise lose the whole compilation to report_fatal_error, with no
+/// source location and no way to recover.
+static void reportUnsupportedAlias(const GlobalAlias &GA, const char *Reason) {
+ const std::string Msg =
+ ("unsupported alias '" + GA.getName() + "': " + Reason).str();
+ LLVMContext &Ctx = GA.getContext();
+ if (const auto *F = dyn_cast_or_null<Function>(GA.getAliaseeObject()))
+ Ctx.diagnose(DiagnosticInfoUnsupported(*F, Msg, F->getSubprogram()));
+ else
+ Ctx.emitError(Msg);
+}
+
void NVPTXAsmPrinter::emitAliasDeclaration(const GlobalAlias *GA,
raw_ostream &O) {
- const Function *F = dyn_cast_or_null<Function>(GA->getAliaseeObject());
- if (!F || isKernelFunction(*F) || F->isDeclaration())
- report_fatal_error(
- "NVPTX aliasee must be a non-kernel function definition");
-
- if (GA->hasLinkOnceLinkage() || GA->hasWeakLinkage() ||
- GA->hasAvailableExternallyLinkage() || GA->hasCommonLinkage())
- report_fatal_error("NVPTX aliasee must not be '.weak'");
+ if (const char *Reason = getAliasUnsupportedReason(*GA)) {
+ reportUnsupportedAlias(*GA, Reason);
+ return;
+ }
- emitDeclarationWithName(F, getSymbol(GA), O);
+ emitDeclarationWithName(cast<Function>(GA->getAliaseeObject()), getSymbol(GA),
+ O);
}
void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) {
@@ -1311,6 +1344,11 @@ void NVPTXAsmPrinter::emitGlobals(const Module &M) {
}
void NVPTXAsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
+ // Already diagnosed by emitAliasDeclaration, which runs first. Emitting the
+ // directive anyway would name a declaration that was deliberately skipped.
+ if (getAliasUnsupportedReason(GA))
+ return;
+
getTargetStreamer()->emitAliasDirective(getSymbol(&GA),
getSymbol(GA.getAliaseeObject()));
}
diff --git a/llvm/test/CodeGen/NVPTX/alias-errors.ll b/llvm/test/CodeGen/NVPTX/alias-errors.ll
index 722459a9e34b5..3fce2406927ce 100644
--- a/llvm/test/CodeGen/NVPTX/alias-errors.ll
+++ b/llvm/test/CodeGen/NVPTX/alias-errors.ll
@@ -1,9 +1,29 @@
; RUN: not --crash llc < %s -mtriple=nvptx64 -mcpu=sm_30 -mattr=+ptx43 2>&1 | FileCheck %s --check-prefix=ATTR
; RUN: not --crash llc < %s -mtriple=nvptx64 -mcpu=sm_20 -mattr=+ptx63 2>&1 | FileCheck %s --check-prefix=ATTR
-; RUN: not --crash llc < %s -mtriple=nvptx64 -mcpu=sm_30 -mattr=+ptx63 2>&1 | FileCheck %s --check-prefix=ALIAS
+; RUN: not llc < %s -mtriple=nvptx64 -mcpu=sm_30 -mattr=+ptx63 2>&1 | FileCheck %s --check-prefix=ALIAS
; ATTR: .alias requires PTX version >= 6.3 and sm_30
-; ALIAS: NVPTX aliasee must be a non-kernel function
+; PTX's .alias directive accepts "non-entry function symbols" only, and requires
+; the aliasee to be defined in the same module, so none of the aliases below can
+; be lowered. Report them instead of aborting: a frontend that emits an alias
+; for every export would otherwise lose the whole compilation to a crash.
+
+; ALIAS-DAG: error: unsupported alias 'not_a_function': PTX can only alias a function
+; ALIAS-DAG: error: {{.*}}unsupported alias 'kernel_alias': PTX .alias requires non-entry functions, so a kernel cannot be aliased
+; ALIAS-DAG: error: {{.*}}unsupported alias 'weak_alias': PTX forbids a .weak aliasee
+
@a = global i32 42, align 8
- at b = internal alias i32, ptr @a
+ at not_a_function = internal alias i32, ptr @a
+
+define ptx_kernel void @the_kernel(ptr %p) {
+ store i32 0, ptr %p
+ ret void
+}
+ at kernel_alias = alias void (ptr), ptr @the_kernel
+
+define void @plain_func(ptr %p) {
+ store i32 1, ptr %p
+ ret void
+}
+ at weak_alias = weak alias void (ptr), ptr @plain_func
More information about the llvm-commits
mailing list