[clang] [llvm] [GOFF] Implement lowerConstant/emitGlobalVariables/emitGlobalAlias for z/OS (PR #169362)
Tony Tao via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 5 11:53:00 PST 2026
https://github.com/tltao updated https://github.com/llvm/llvm-project/pull/169362
>From 6eacc324f4b35800ce2b77cfb98f943888c6404a Mon Sep 17 00:00:00 2001
From: Tony Tao <tonytao at ca.ibm.com>
Date: Wed, 19 Nov 2025 19:55:35 +0000
Subject: [PATCH 1/5] Implement emitGlobalVariable and lowerConstant
---
clang/lib/Lex/HeaderMap.cpp | 1 +
llvm/lib/Support/VirtualOutputBackends.cpp | 1 +
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 73 +++++++++++++++++++
llvm/lib/Target/SystemZ/SystemZAsmPrinter.h | 2 +
4 files changed, 77 insertions(+)
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp
index a7b670f00ac6e..588b32ee9ca8e 100644
--- a/clang/lib/Lex/HeaderMap.cpp
+++ b/clang/lib/Lex/HeaderMap.cpp
@@ -18,6 +18,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SystemZ/zOSSupport.h"
#include <cstring>
#include <memory>
#include <optional>
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index d7a91dfa1471f..30cd42aa72558 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -285,6 +285,7 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional<int> &FD) {
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
int NewFD;
+ sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
SmallString<128> UniquePath;
sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
if (std::error_code EC =
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 444bc578a3fab..fa2c2fe535fe7 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -1113,6 +1113,24 @@ void SystemZAsmPrinter::emitEndOfAsmFile(Module &M) {
if (TT.isOSzOS()) {
emitADASection();
emitIDRLSection(M);
+ // Emit EXTRN declarations.
+ OutStreamer->pushSection();
+ for (auto &GO : M.global_objects()) {
+ if (GO.isDeclaration()) {
+ MCSymbol *Sym = TM.getSymbol(&GO);
+ OutStreamer->emitSymbolAttribute(Sym, GO.hasExternalWeakLinkage()
+ ? MCSA_WeakReference
+ : MCSA_Global);
+ OutStreamer->emitSymbolAttribute(Sym, isa<Function>(GO) ? MCSA_Code
+ : MCSA_Data);
+ llvm::dbgs() << "TONY emitting " << Sym->getName() << "\n";
+ }
+ }
+ OutStreamer->switchSection(
+ static_cast<MCSectionGOFF *>(getObjFileLowering().getTextSection())
+ ->getParent());
+ getTargetStreamer()->emitExterns();
+ OutStreamer->popSection();
}
emitAttributes(M);
}
@@ -1704,6 +1722,61 @@ void SystemZAsmPrinter::emitPPA2(Module &M) {
OutStreamer->popSection();
}
+void SystemZAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
+ if (TM.getTargetTriple().isOSzOS()) {
+ auto *Sym = getSymbol(GV);
+ OutStreamer->emitSymbolAttribute(Sym, MCSA_Data);
+ }
+
+ AsmPrinter::emitGlobalVariable(GV);
+}
+
+const MCExpr *SystemZAsmPrinter::lowerConstant(const Constant *CV,
+ const Constant *BaseCV,
+ uint64_t Offset) {
+ const Triple &TargetTriple = TM.getTargetTriple();
+
+ if (TargetTriple.isOSzOS()) {
+ const GlobalAlias *GA = dyn_cast<GlobalAlias>(CV);
+ const GlobalVariable *GV = dyn_cast<GlobalVariable>(CV);
+ const Function *FV = dyn_cast<Function>(CV);
+ bool IsFunc = !GV && (FV || (GA && isa<Function>(GA->getAliaseeObject())));
+
+ MCSymbol *Sym = NULL;
+
+ if (GA)
+ Sym = getSymbol(GA);
+ else if (IsFunc)
+ Sym = getSymbol(FV);
+ else if (GV)
+ Sym = getSymbol(GV);
+
+ if (IsFunc) {
+ OutStreamer->emitSymbolAttribute(Sym, MCSA_Code);
+ if (FV->hasExternalLinkage()) {
+ return MCSpecifierExpr::create(MCSymbolRefExpr::create(Sym, OutContext),
+ SystemZ::S_VCon, OutContext);
+ }
+ // Trigger creation of function descriptor in ADA for internal
+ // functions.
+ unsigned Disp = ADATable.insert(Sym, SystemZII::MO_ADA_DIRECT_FUNC_DESC);
+ return MCBinaryExpr::createAdd(
+ MCSpecifierExpr::create(
+ MCSymbolRefExpr::create(
+ getObjFileLowering().getADASection()->getBeginSymbol(),
+ OutContext),
+ SystemZ::S_None, OutContext),
+ MCConstantExpr::create(Disp, OutContext), OutContext);
+ }
+ if (Sym) {
+ OutStreamer->emitSymbolAttribute(Sym, MCSA_Data);
+ return MCSymbolRefExpr::create(Sym, OutContext);
+ }
+ }
+
+ return AsmPrinter::lowerConstant(CV);
+}
+
void SystemZAsmPrinter::emitFunctionEntryLabel() {
const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
index cb101e472824f..5c7658dd3055b 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
@@ -130,6 +130,8 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
void emitFunctionEntryLabel() override;
void emitFunctionBodyEnd() override;
void emitStartOfAsmFile(Module &M) override;
+ void emitGlobalVariable(const GlobalVariable *GV) override;
+ const MCExpr *lowerConstant(const Constant *CV, const Constant *BaseCV = nullptr, uint64_t Offset = 0) override;
private:
void emitCallInformation(CallType CT);
>From d495f47632f6de0306c1fde2d4ed15c788fd8ac3 Mon Sep 17 00:00:00 2001
From: Tony Tao <tonytao at ca.ibm.com>
Date: Wed, 19 Nov 2025 19:57:30 +0000
Subject: [PATCH 2/5] undo accdiental commit
---
llvm/lib/Support/VirtualOutputBackends.cpp | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index 30cd42aa72558..a4701cd79dfce 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -285,11 +285,10 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional<int> &FD) {
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
int NewFD;
- sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
SmallString<128> UniquePath;
sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
if (std::error_code EC =
- sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF))
+ sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath))
return make_error<TempFileOutputError>(ModelPath, OutputPath, EC);
if (Config.getDiscardOnSignal())
@@ -333,7 +332,13 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
// Not using a temporary file. Open the final output file.
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
int NewFD;
- sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
+ sys::fs::OpenFlags OF = sys::fs::OF_None;
+ if (Config.getTextWithCRLF())
+ OF |= sys::fs::OF_TextWithCRLF;
+ else if (Config.getText())
+ OF |= sys::fs::OF_Text;
+ if (Config.getAppend())
+ OF |= sys::fs::OF_Append;
if (std::error_code EC = sys::fs::openFileForWrite(
OutputPath, NewFD, sys::fs::CD_CreateAlways, OF))
return convertToOutputError(OutputPath, EC);
>From 0fc2b3090b920ad927b97967f4a3c505eea5ca2a Mon Sep 17 00:00:00 2001
From: Tony Tao <tonytao at ca.ibm.com>
Date: Fri, 21 Nov 2025 13:58:15 -0500
Subject: [PATCH 3/5] implement more features
---
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 36 ++++++++++++++++++-
llvm/lib/Target/SystemZ/SystemZAsmPrinter.h | 1 +
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index fa2c2fe535fe7..e6ec665473de0 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -347,7 +347,7 @@ void SystemZAsmPrinter::emitInstruction(const MachineInstr *MI) {
case SystemZ::ADA_ENTRY: {
const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
- uint32_t Disp = ADATable.insert(MI->getOperand(1));
+ uint32_t Disp = ADATable.insert(MI->getOperand(1));
Register TargetReg = MI->getOperand(0).getReg();
Register ADAReg = MI->getOperand(2).getReg();
@@ -373,6 +373,8 @@ void SystemZAsmPrinter::emitInstruction(const MachineInstr *MI) {
Disp = 0;
Op = Op0;
}
+ OutStreamer->AddComment(Twine("Loading from ADA at offset ")
+ .concat(utostr(Disp)));
EmitToStreamer(*OutStreamer, MCInstBuilder(Op)
.addReg(TargetReg)
.addReg(ADAReg)
@@ -1777,6 +1779,38 @@ const MCExpr *SystemZAsmPrinter::lowerConstant(const Constant *CV,
return AsmPrinter::lowerConstant(CV);
}
+void SystemZAsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
+ if (!TM.getTargetTriple().isOSzOS()) {
+ AsmPrinter::emitGlobalAlias(M, GA);
+ return;
+ }
+
+ MCSymbol *Name = getSymbol(&GA);
+ bool IsFunc = isa<Function>(GA.getAliasee()->stripPointerCasts());
+
+ if (GA.hasExternalLinkage() || !MAI->getWeakRefDirective())
+ OutStreamer->emitSymbolAttribute(Name, MCSA_Global);
+ else if (GA.hasWeakLinkage() || GA.hasLinkOnceLinkage())
+ OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference);
+ else
+ assert(GA.hasLocalLinkage() && "Invalid alias linkage");
+
+ emitVisibility(Name, GA.getVisibility());
+
+ const MCExpr *Expr;
+
+ // For XPLINK, create a VCON relocation in case of a function, and
+ // a direct reference else.
+ MCSymbol *Sym = getSymbol(GA.getAliaseeObject());
+ if (IsFunc)
+ Expr = MCSpecifierExpr::create(MCSymbolRefExpr::create(Sym, OutContext),
+ SystemZ::S_VCon, OutContext);
+ else
+ Expr = MCSymbolRefExpr::create(Sym, OutContext);
+
+ OutStreamer->emitAssignment(Name, Expr);
+}
+
void SystemZAsmPrinter::emitFunctionEntryLabel() {
const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
index 5c7658dd3055b..003e9af7c2ca8 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
@@ -131,6 +131,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
void emitFunctionBodyEnd() override;
void emitStartOfAsmFile(Module &M) override;
void emitGlobalVariable(const GlobalVariable *GV) override;
+ void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
const MCExpr *lowerConstant(const Constant *CV, const Constant *BaseCV = nullptr, uint64_t Offset = 0) override;
private:
>From a364f19299e97c2618442bf635139db632e063af Mon Sep 17 00:00:00 2001
From: Tony Tao <tonytao at ca.ibm.com>
Date: Mon, 24 Nov 2025 11:52:05 -0500
Subject: [PATCH 4/5] Syntax fix and remove comment
---
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index e6ec665473de0..936f80731c39b 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -347,7 +347,7 @@ void SystemZAsmPrinter::emitInstruction(const MachineInstr *MI) {
case SystemZ::ADA_ENTRY: {
const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
- uint32_t Disp = ADATable.insert(MI->getOperand(1));
+ uint32_t Disp = ADATable.insert(MI->getOperand(1));
Register TargetReg = MI->getOperand(0).getReg();
Register ADAReg = MI->getOperand(2).getReg();
@@ -1125,7 +1125,6 @@ void SystemZAsmPrinter::emitEndOfAsmFile(Module &M) {
: MCSA_Global);
OutStreamer->emitSymbolAttribute(Sym, isa<Function>(GO) ? MCSA_Code
: MCSA_Data);
- llvm::dbgs() << "TONY emitting " << Sym->getName() << "\n";
}
}
OutStreamer->switchSection(
>From 9e401cfa7c28979fc366b90634a9c83178e2451d Mon Sep 17 00:00:00 2001
From: Tony Tao <tonytao at ca.ibm.com>
Date: Mon, 5 Jan 2026 14:52:43 -0500
Subject: [PATCH 5/5] undo commit fix
---
clang/lib/Lex/HeaderMap.cpp | 1 -
llvm/lib/Support/VirtualOutputBackends.cpp | 10 ++--------
2 files changed, 2 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp
index 588b32ee9ca8e..a7b670f00ac6e 100644
--- a/clang/lib/Lex/HeaderMap.cpp
+++ b/clang/lib/Lex/HeaderMap.cpp
@@ -18,7 +18,6 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/SystemZ/zOSSupport.h"
#include <cstring>
#include <memory>
#include <optional>
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index a4701cd79dfce..d7a91dfa1471f 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -288,7 +288,7 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional<int> &FD) {
SmallString<128> UniquePath;
sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
if (std::error_code EC =
- sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath))
+ sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF))
return make_error<TempFileOutputError>(ModelPath, OutputPath, EC);
if (Config.getDiscardOnSignal())
@@ -332,13 +332,7 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
// Not using a temporary file. Open the final output file.
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
int NewFD;
- sys::fs::OpenFlags OF = sys::fs::OF_None;
- if (Config.getTextWithCRLF())
- OF |= sys::fs::OF_TextWithCRLF;
- else if (Config.getText())
- OF |= sys::fs::OF_Text;
- if (Config.getAppend())
- OF |= sys::fs::OF_Append;
+ sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
if (std::error_code EC = sys::fs::openFileForWrite(
OutputPath, NewFD, sys::fs::CD_CreateAlways, OF))
return convertToOutputError(OutputPath, EC);
More information about the cfe-commits
mailing list