summaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/AsmPrinter
diff options
context:
space:
mode:
authorMichael Kruse <llvm-project@meinersbur.de>2025-01-03 10:22:51 +0100
committerMichael Kruse <llvm-project@meinersbur.de>2025-01-03 10:22:51 +0100
commit38500d63e14ce340236840f60d356cdefb56a52c (patch)
tree17edbec446ce9b50d2f215a483b83afb293a635d /llvm/lib/CodeGen/AsmPrinter
parent1a3d5daaef7a6a63448a497da3eff7fc9e23df26 (diff)
parent27f30029741ecf023baece7b3dde1ff9011ffefc (diff)
Merge branch 'main' into users/meinersbur/flang_runtime_split-headersusers/meinersbur/flang_runtime_split-headers
Diffstat (limited to 'llvm/lib/CodeGen/AsmPrinter')
-rw-r--r--llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp107
-rw-r--r--llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp42
-rw-r--r--llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp3
3 files changed, 93 insertions, 59 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 3072edc5088e..7bd3fb33b47d 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -503,13 +503,7 @@ bool AsmPrinter::doInitialization(Module &M) {
// don't, this at least helps the user find where a global came from.
if (MAI->hasSingleParameterDotFile()) {
// .file "foo.c"
-
- SmallString<128> FileName;
- if (MAI->hasBasenameOnlyForFileDirective())
- FileName = llvm::sys::path::filename(M.getSourceFileName());
- else
- FileName = M.getSourceFileName();
- if (MAI->hasFourStringsDotFile()) {
+ if (MAI->isAIX()) {
const char VerStr[] =
#ifdef PACKAGE_VENDOR
PACKAGE_VENDOR " "
@@ -520,9 +514,10 @@ bool AsmPrinter::doInitialization(Module &M) {
#endif
;
// TODO: Add timestamp and description.
- OutStreamer->emitFileDirective(FileName, VerStr, "", "");
+ OutStreamer->emitFileDirective(M.getSourceFileName(), VerStr, "", "");
} else {
- OutStreamer->emitFileDirective(FileName);
+ OutStreamer->emitFileDirective(
+ llvm::sys::path::filename(M.getSourceFileName()));
}
}
@@ -531,7 +526,8 @@ bool AsmPrinter::doInitialization(Module &M) {
if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
emitModuleCommandLines(M);
// Now we can generate section information.
- OutStreamer->initSections(false, *TM.getMCSubtargetInfo());
+ OutStreamer->switchSection(
+ OutContext.getObjectFileInfo()->getTextSection());
// To work around an AIX assembler and/or linker bug, generate
// a rename for the default text-section symbol name. This call has
@@ -966,11 +962,10 @@ void AsmPrinter::emitFunctionHeader() {
MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
OutStreamer->switchSection(MF->getSection());
- if (!MAI->hasVisibilityOnlyWithLinkage())
- emitVisibility(CurrentFnSym, F.getVisibility());
-
- if (MAI->needsFunctionDescriptors())
+ if (MAI->isAIX())
emitLinkage(&F, CurrentFnDescSym);
+ else
+ emitVisibility(CurrentFnSym, F.getVisibility());
emitLinkage(&F, CurrentFnSym);
if (MAI->hasFunctionAlignment())
@@ -1030,7 +1025,7 @@ void AsmPrinter::emitFunctionHeader() {
// to emit their specific function descriptor. Right now it is only used by
// the AIX target. The PowerPC 64-bit V1 ELF target also uses function
// descriptors and should be converted to use this hook as well.
- if (MAI->needsFunctionDescriptors())
+ if (MAI->isAIX())
emitFunctionDescriptor();
// Emit the CurrentFnSym. This is a virtual function to allow targets to do
@@ -1791,7 +1786,7 @@ void AsmPrinter::emitFunctionBody() {
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
if (!MDT) {
OwnedMDT = std::make_unique<MachineDominatorTree>();
- OwnedMDT->getBase().recalculate(*MF);
+ OwnedMDT->recalculate(*MF);
MDT = OwnedMDT.get();
}
@@ -1800,7 +1795,7 @@ void AsmPrinter::emitFunctionBody() {
MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
if (!MLI) {
OwnedMLI = std::make_unique<MachineLoopInfo>();
- OwnedMLI->analyze(MDT->getBase());
+ OwnedMLI->analyze(*MDT);
MLI = OwnedMLI.get();
}
}
@@ -2233,9 +2228,6 @@ void AsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
// point, all the extra label is emitted, we just have to emit linkage for
// those labels.
if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
- assert(MAI->hasVisibilityOnlyWithLinkage() &&
- "Visibility should be handled with emitLinkage() on AIX.");
-
// Linkage for alias of global variable has been emitted.
if (isa<GlobalVariable>(GA.getAliaseeObject()))
return;
@@ -2406,12 +2398,53 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
OutStreamer->emitBinaryData(Buf);
}
+static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
+ Constant *Initializer = G->getInitializer();
+ uint64_t SizeInBytes =
+ M.getDataLayout().getTypeAllocSize(Initializer->getType());
+
+ uint64_t NewSize = alignTo(SizeInBytes, 16);
+ if (SizeInBytes != NewSize) {
+ // Pad the initializer out to the next multiple of 16 bytes.
+ llvm::SmallVector<uint8_t> Init(NewSize - SizeInBytes, 0);
+ Constant *Padding = ConstantDataArray::get(M.getContext(), Init);
+ Initializer = ConstantStruct::getAnon({Initializer, Padding});
+ auto *NewGV = new GlobalVariable(
+ M, Initializer->getType(), G->isConstant(), G->getLinkage(),
+ Initializer, "", G, G->getThreadLocalMode(), G->getAddressSpace());
+ NewGV->copyAttributesFrom(G);
+ NewGV->setComdat(G->getComdat());
+ NewGV->copyMetadata(G, 0);
+
+ NewGV->takeName(G);
+ G->replaceAllUsesWith(NewGV);
+ G->eraseFromParent();
+ G = NewGV;
+ }
+
+ if (G->getAlign().valueOrOne() < 16)
+ G->setAlignment(Align(16));
+
+ // Ensure that tagged globals don't get merged by ICF - as they should have
+ // different tags at runtime.
+ G->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
+}
+
bool AsmPrinter::doFinalization(Module &M) {
// Set the MachineFunction to nullptr so that we can catch attempted
// accesses to MF specific features at the module level and so that
// we can conditionalize accesses based on whether or not it is nullptr.
MF = nullptr;
+ std::vector<GlobalVariable *> GlobalsToTag;
+ for (GlobalVariable &G : M.globals()) {
+ if (G.isDeclaration() || !G.isTagged())
+ continue;
+ GlobalsToTag.push_back(&G);
+ }
+ for (GlobalVariable *G : GlobalsToTag)
+ tagGlobalDefinition(M, G);
+
// Gather all GOT equivalent globals in the module. We really need two
// passes over the globals: one to compute and another to avoid its emission
// in EmitGlobalVariable, otherwise we would not be able to handle cases
@@ -2688,7 +2721,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
HasNoSplitStack = true;
// Get the function symbol.
- if (!MAI->needsFunctionDescriptors()) {
+ if (!MAI->isAIX()) {
CurrentFnSym = getSymbol(&MF.getFunction());
} else {
assert(TM.getTargetTriple().isOSAIX() &&
@@ -3602,10 +3635,11 @@ static void emitGlobalConstantArray(const DataLayout &DL,
static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
-static void emitGlobalConstantVector(const DataLayout &DL,
- const ConstantVector *CV, AsmPrinter &AP,
+static void emitGlobalConstantVector(const DataLayout &DL, const Constant *CV,
+ AsmPrinter &AP,
AsmPrinter::AliasMapTy *AliasList) {
- Type *ElementType = CV->getType()->getElementType();
+ auto *VTy = cast<FixedVectorType>(CV->getType());
+ Type *ElementType = VTy->getElementType();
uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
uint64_t EmittedSize;
@@ -3618,7 +3652,7 @@ static void emitGlobalConstantVector(const DataLayout &DL,
Type *IntT =
IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
- ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
+ ConstantExpr::getBitCast(const_cast<Constant *>(CV), IntT), DL));
if (!CI) {
report_fatal_error(
"Cannot lower vector global with unusual element type");
@@ -3627,12 +3661,11 @@ static void emitGlobalConstantVector(const DataLayout &DL,
emitGlobalConstantLargeInt(CI, AP);
EmittedSize = DL.getTypeStoreSize(CV->getType());
} else {
- for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
+ for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
- emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
+ emitGlobalConstantImpl(DL, CV->getAggregateElement(I), AP);
}
- EmittedSize =
- DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
+ EmittedSize = DL.getTypeAllocSize(ElementType) * VTy->getNumElements();
}
unsigned Size = DL.getTypeAllocSize(CV->getType());
@@ -3902,8 +3935,10 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
return AP.OutStreamer->emitZeros(Size);
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
- const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
+ if (isa<VectorType>(CV->getType()))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
+ const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
if (StoreSize <= 8) {
if (AP.isVerbose())
AP.OutStreamer->getCommentOS()
@@ -3920,8 +3955,12 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
return;
}
- if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
- return emitGlobalConstantFP(CFP, AP);
+ if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
+ if (isa<VectorType>(CV->getType()))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
+ else
+ return emitGlobalConstantFP(CFP, AP);
+ }
if (isa<ConstantPointerNull>(CV)) {
AP.OutStreamer->emitIntValue(0, Size);
@@ -3953,8 +3992,8 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
}
}
- if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
- return emitGlobalConstantVector(DL, V, AP, AliasList);
+ if (isa<ConstantVector>(CV))
+ return emitGlobalConstantVector(DL, CV, AP, AliasList);
// Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
// thread the streamer with EmitValue.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
index 6fe8d0e0af99..59fc4cfc23e1 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
@@ -153,7 +153,7 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
// FIXME: Should this happen for `asm inteldialect` as well?
- if (!InputIsIntelDialect && MAI->getEmitGNUAsmStartIndentationMarker())
+ if (!InputIsIntelDialect && !MAI->isHLASM())
OS << '\t';
while (*LastEmitted) {
@@ -312,10 +312,10 @@ static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
}
}
if (Error) {
- std::string msg;
- raw_string_ostream Msg(msg);
- Msg << "invalid operand in inline asm: '" << AsmStr << "'";
- MMI->getModule()->getContext().emitError(LocCookie, msg);
+ const Function &Fn = MI->getMF()->getFunction();
+ Fn.getContext().diagnose(DiagnosticInfoInlineAsm(
+ LocCookie,
+ "invalid operand in inline asm: '" + Twine(AsmStr) + "'"));
}
}
break;
@@ -347,20 +347,11 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
// enabled, so we use emitRawComment.
OutStreamer->emitRawComment(MAI->getInlineAsmStart());
- // Get the !srcloc metadata node if we have it, and decode the loc cookie from
- // it.
- uint64_t LocCookie = 0;
- const MDNode *LocMD = nullptr;
- for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
- if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
- LocMD->getNumOperands() != 0) {
- if (const ConstantInt *CI =
- mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
- LocCookie = CI->getZExtValue();
- break;
- }
- }
- }
+ const MDNode *LocMD = MI->getLocCookieMD();
+ uint64_t LocCookie =
+ LocMD
+ ? mdconst::extract<ConstantInt>(LocMD->getOperand(0))->getZExtValue()
+ : 0;
// Emit the inline asm to a temporary string so we can emit it through
// EmitInlineAsm.
@@ -397,20 +388,23 @@ void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
Msg += LS;
Msg += TRI->getRegAsmName(RR);
}
+
+ const Function &Fn = MF->getFunction();
const char *Note =
"Reserved registers on the clobber list may not be "
"preserved across the asm statement, and clobbering them may "
"lead to undefined behaviour.";
- MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
- LocCookie, Msg, DiagnosticSeverity::DS_Warning));
- MMI->getModule()->getContext().diagnose(
+ LLVMContext &Ctx = Fn.getContext();
+ Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, Msg,
+ DiagnosticSeverity::DS_Warning));
+ Ctx.diagnose(
DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
for (const Register RR : RestrRegs) {
if (std::optional<std::string> reason =
TRI->explainReservedReg(*MF, RR)) {
- MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
- LocCookie, *reason, DiagnosticSeverity::DS_Note));
+ Ctx.diagnose(DiagnosticInfoInlineAsm(LocCookie, *reason,
+ DiagnosticSeverity::DS_Note));
}
}
}
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp
index 087ee02a7f2b..4fac4bbc9847 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp
@@ -50,7 +50,8 @@ void DwarfCFIException::endModule() {
// Emit indirect reference table for all used personality functions
for (const GlobalValue *Personality : Personalities) {
MCSymbol *Sym = Asm->getSymbol(Personality);
- TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
+ TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym,
+ Asm->MMI);
}
Personalities.clear();
}