Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/BinaryFormat/GoObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
namespace llvm {
namespace GoObj {

// LLVM global names carry this suffix when they identify the ABI0 form of a
// Go function. GoObj serialization strips it and records ABI0 separately.
inline constexpr char ABI0SymbolSuffix[] = "<ABI0>";

// "GoStackG" encoded as the stable statepoint identifier for the pre-frame
// runtime.morestack slow path.
inline constexpr uint64_t StackGrowthStatepointID = 0x476f537461636b47ULL;
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/CodeGen/GoCallingConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
namespace llvm {

class CallBase;
class MachineFunction;
class MachineInstrBuilder;
class Type;

namespace goabi {
Expand Down Expand Up @@ -111,6 +113,12 @@ EntryArgsInfo computeEntryArgsInfo(ArrayRef<Type *> ArgTys,
bool isIntegerPiece(Type *Ty);
bool isFloatingPiece(Type *Ty);

/// Add a late target-inserted call operand for the ABI0 form of \p SymbolName.
/// ABI0 identity is encoded directly in the MC symbol name; the GoObj writer
/// strips the reserved suffix and records ABI0 in the object symbol.
void addGoObjABI0Callee(MachineInstrBuilder &MIB, MachineFunction &MF,
StringRef SymbolName);

} // namespace goabi
} // namespace llvm

Expand Down
29 changes: 6 additions & 23 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,8 @@ class MCContext {
/// Bindings of names to symbol table values.
SymbolTable Symbols;

/// Go object symbol ABI overrides keyed by MC symbol.
DenseMap<const MCSymbol *, uint16_t> GoObjSymbolABIs;

/// Go object linker names for MC symbols whose LLVM names were made unique.
DenseMap<const MCSymbol *, std::string> GoObjSymbolNames;
/// MC symbols known to denote Go functions, including declarations.
DenseSet<const MCSymbol *> GoObjFunctionSymbols;

/// Package-local indices assigned to Go object definitions by the frontend.
DenseMap<const MCSymbol *, uint32_t> GoObjPackageSymbolIndexes;
Expand Down Expand Up @@ -692,26 +689,12 @@ class MCContext {
/// inline assembly.
LLVM_ABI void registerInlineAsmLabel(MCSymbol *Sym);

void setGoObjSymbolABI(const MCSymbol *Sym, uint16_t ABI) {
GoObjSymbolABIs[Sym] = ABI;
}

std::optional<uint16_t> getGoObjSymbolABI(const MCSymbol *Sym) const {
auto It = GoObjSymbolABIs.find(Sym);
if (It == GoObjSymbolABIs.end())
return std::nullopt;
return It->second;
void setGoObjFunctionSymbol(const MCSymbol *Sym) {
GoObjFunctionSymbols.insert(Sym);
}

void setGoObjSymbolName(const MCSymbol *Sym, StringRef Name) {
GoObjSymbolNames[Sym] = Name.str();
}

StringRef getGoObjSymbolName(const MCSymbol *Sym) const {
auto It = GoObjSymbolNames.find(Sym);
if (It == GoObjSymbolNames.end())
return {};
return It->second;
bool isGoObjFunctionSymbol(const MCSymbol *Sym) const {
return GoObjFunctionSymbols.contains(Sym);
}

void setGoObjPackageSymbolIndex(const MCSymbol *Sym, uint32_t Index) {
Expand Down
42 changes: 19 additions & 23 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,6 @@ bool AsmPrinter::doInitialization(Module &M) {

if (Target.isOSBinFormatGoObj()) {
collectGoObjModuleMetadata(*this, M);
for (const Function &F : M) {
if (!F.isDeclaration())
continue;
if (goabi::isGoABIInternalCallingConv(F.getCallingConv()))
OutContext.setGoObjSymbolABI(getSymbol(&F), GoObj::SymABIInternal);
else if (goabi::isGoABI0CallingConv(F.getCallingConv()))
OutContext.setGoObjSymbolABI(getSymbol(&F), GoObj::SymABI0);
}
}

// On AIX, we delay emitting any section information until
Expand Down Expand Up @@ -1049,17 +1041,26 @@ static void collectGoObjModuleMetadata(AsmPrinter &AP, const Module &M) {
AP.OutContext.setGoObjImports(std::move(ParsedImports));
}

for (const GlobalObject &GO : M.global_objects()) {
const MDNode *MD = GO.getMetadata("goobj.symbol.name");
if (!MD)
// The LLVM symbol name is the sole carrier of ABI0 object identity. Keep the
// calling convention as an independent lowering contract and reject stale
// metadata or mismatched names instead of trying to repair either one.
for (const GlobalObject &GO : M.global_objects())
if (GO.getMetadata("goobj.symbol.name"))
report_fatal_error(
"!goobj.symbol.name is obsolete; encode ABI0 in the symbol name");

for (const Function &F : M) {
if (F.isIntrinsic())
continue;
if (MD->getNumOperands() != 1)
report_fatal_error("expected !goobj.symbol.name to have one operand");
StringRef Name =
getGoObjMetadataString(MD->getOperand(0), "goobj.symbol.name");
if (Name.empty())
report_fatal_error("invalid !goobj.symbol.name attachment");
AP.OutContext.setGoObjSymbolName(AP.getSymbol(&GO), Name);
bool HasABI0Suffix = F.getName().ends_with(GoObj::ABI0SymbolSuffix);
bool IsABI0 = goabi::isGoABI0CallingConv(F.getCallingConv());
bool IsABIInternal =
goabi::isGoABIInternalCallingConv(F.getCallingConv());
if (HasABI0Suffix != IsABI0)
report_fatal_error(
"Go ABI0 calling convention and <ABI0> symbol suffix disagree");
if (IsABI0 || IsABIInternal)
AP.OutContext.setGoObjFunctionSymbol(AP.getSymbol(&F));
}

for (const GlobalObject &GO : M.global_objects()) {
Expand Down Expand Up @@ -3750,11 +3751,6 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
if (std::optional<std::pair<uint8_t, uint8_t>> Info =
getGoObjFunctionInfo(F))
OutContext.setGoObjFunctionInfo(CurrentFnSym, Info->first, Info->second);
if (goabi::isGoABIInternalCallingConv(F.getCallingConv()))
OutContext.setGoObjSymbolABI(CurrentFnSym, GoObj::SymABIInternal);
else if (goabi::isGoABI0CallingConv(F.getCallingConv()))
OutContext.setGoObjSymbolABI(CurrentFnSym, GoObj::SymABI0);

const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
uint64_t StackSize =
FrameInfo.getStackSize() + FrameInfo.getUnsafeStackSize();
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/CodeGen/GoCallingConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@

#include "llvm/CodeGen/GoCallingConv.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/ErrorHandling.h"
#include <limits>
#include <string>

using namespace llvm;

namespace llvm::goabi {

void addGoObjABI0Callee(MachineInstrBuilder &MIB, MachineFunction &MF,
StringRef SymbolName) {
if (SymbolName.empty() || SymbolName.ends_with(GoObj::ABI0SymbolSuffix))
report_fatal_error("invalid logical Go ABI0 symbol name");
std::string StorageName = (SymbolName + GoObj::ABI0SymbolSuffix).str();
MIB.addExternalSymbol(MF.createExternalSymbolName(StorageName));
}

static bool isPaddingType(Type *Ty) {
auto *ST = dyn_cast<StructType>(Ty);
if (!ST || !ST->hasName() || ST->getName() != PadTypeName)
Expand Down
50 changes: 40 additions & 10 deletions llvm/lib/MC/GoObjObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,9 +1512,26 @@ void GoObjObjectWriter::recordRelocation(const MCFragment &F,
uint64_t GoObjObjectWriter::writeObject() {
const uint64_t StartOffset = OS.tell();

StringRef ABI0Suffix = GoObj::ABI0SymbolSuffix;
auto HasABI0Suffix = [&](const MCSymbol *Sym) {
return Sym && Sym->getName().ends_with(ABI0Suffix);
};
auto GetSymbolName = [&](const MCSymbol *Sym) -> StringRef {
StringRef Name = Asm->getContext().getGoObjSymbolName(Sym);
return Name.empty() ? Sym->getName() : Name;
StringRef Name = Sym->getName();
if (!HasABI0Suffix(Sym))
return Name;
Name = Name.drop_back(ABI0Suffix.size());
if (Name.empty() || Name.ends_with(ABI0Suffix))
report_fatal_error("invalid Go ABI0 symbol name");
return Name;
};
auto GetSymbolABI = [&](const MCSymbol *Sym, bool IsFunction) {
if (HasABI0Suffix(Sym)) {
if (!IsFunction)
report_fatal_error("Go ABI0 suffix requires a function symbol");
return GoObj::SymABI0;
}
return IsFunction ? GoObj::SymABIInternal : GoObj::SymABI0;
};

std::vector<GoObjSymbol> Symbols;
Expand All @@ -1530,6 +1547,8 @@ uint64_t GoObjObjectWriter::writeObject() {
for (const MCSymbol &Symbol : Asm->symbols()) {
if (!Symbol.isCommon())
continue;
if (HasABI0Suffix(&Symbol))
report_fatal_error("Go ABI0 suffix requires a function symbol");
GoObjSymbol GoSym;
GoSym.Name = GetSymbolName(&Symbol).str();
GoSym.Symbol = &Symbol;
Expand Down Expand Up @@ -1591,10 +1610,8 @@ uint64_t GoObjObjectWriter::writeObject() {
Data = ArrayRef<char>(Contents.data() + Begin, Size);
}
uint8_t Type = getGoObjSymbolType(&Section);
uint16_t ABI = MCSym
? Asm->getContext().getGoObjSymbolABI(MCSym).value_or(
GoObj::SymABI0)
: GoObj::SymABI0;
uint16_t ABI = GetSymbolABI(
MCSym, MCSym && Asm->getContext().isGoObjFunctionSymbol(MCSym));
uint8_t Flag = 0;
uint8_t Flag2 = 0;
uint32_t Align = 0;
Expand Down Expand Up @@ -2169,13 +2186,12 @@ uint64_t GoObjObjectWriter::writeObject() {

std::vector<GoObjSymbol> NonPkgRefs;
StringMap<uint32_t> NonPkgRefIndexes;
auto GetNonPkgRefSymIdx = [&](const MCSymbol *Sym) {
auto GetNonPkgRefSymIdx = [&](const MCSymbol *Sym, bool IsFunction) {
StringRef Name = GetSymbolName(Sym);
if (Name.empty())
report_fatal_error("GoObj relocation target has an empty name");

uint16_t ABI =
Asm->getContext().getGoObjSymbolABI(Sym).value_or(GoObj::SymABI0);
uint16_t ABI = GetSymbolABI(Sym, IsFunction);
std::string Key = (Name + "#" + Twine(ABI)).str();
auto It = NonPkgRefIndexes.find(Key);
if (It != NonPkgRefIndexes.end())
Expand Down Expand Up @@ -2279,7 +2295,21 @@ uint64_t GoObjObjectWriter::writeObject() {
return GoObjSymRef{GoObj::PkgIdxBuiltin, Metadata->SymIdx};
}
}
return GoObjSymRef{GoObj::PkgIdxNone, GetNonPkgRefSymIdx(Reloc.Symbol)};
bool IsFunction;
switch (Reloc.Type) {
case GoObj::R_CALL:
case GoObj::R_CALLARM:
case GoObj::R_CALLARM64:
case GoObj::R_CALLPOWER:
case GoObj::R_CALLMIPS:
IsFunction = true;
break;
default:
IsFunction = Asm->getContext().isGoObjFunctionSymbol(Reloc.Symbol);
break;
}
return GoObjSymRef{GoObj::PkgIdxNone,
GetNonPkgRefSymIdx(Reloc.Symbol, IsFunction)};
}

report_fatal_error(
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ void MCContext::reset() {

MCSubtargetAllocator.DestroyAll();
InlineAsmUsedLabelNames.clear();
GoObjSymbolABIs.clear();
GoObjSymbolNames.clear();
GoObjFunctionSymbols.clear();
GoObjPackageSymbolIndexes.clear();
GoObjCgoPragmas.clear();
GoObjNonPackageSymbols.clear();
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,6 @@ bool AArch64ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
if (MF.getTarget().getTargetTriple().isOSBinFormatGoObj()) {
MCContext &Ctx = MF.getContext();
MCSymbol *Callee = Ctx.getOrCreateSymbol(WriteBarrierName);
Ctx.setGoObjSymbolABI(Callee, GoObj::SymABIInternal);
Call.addSym(Callee);
} else {
Call.addExternalSymbol(WriteBarrierName);
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,13 +1275,13 @@ checkAArch64GoStackGrowthStatepointContract(const MachineFunction &MF) {

static MachineInstrBuilder buildAArch64GoStackGrowthStatepoint(
MachineFunction &MF, MachineBasicBlock &MBB, const DebugLoc &DL,
const AArch64InstrInfo &TII, const char *Callee) {
const AArch64InstrInfo &TII, StringRef CalleeName) {
MachineInstrBuilder Statepoint =
BuildMI(&MBB, DL, TII.get(TargetOpcode::STATEPOINT))
.addImm(goabi::StackGrowthStatepointID)
.addImm(0)
.addImm(0)
.addExternalSymbol(Callee);
.addImm(0);
goabi::addGoObjABI0Callee(Statepoint, MF, CalleeName);
auto AddConstant = [&](uint64_t Value) {
Statepoint.addImm(StackMaps::ConstantOp).addImm(Value);
};
Expand Down Expand Up @@ -1521,8 +1521,9 @@ static void emitAArch64GoStackCheck(MachineFunction &MF,
MachineInstrBuilder Morestack =
UseStackGrowthStatepoint ? buildAArch64GoStackGrowthStatepoint(
MF, *MorestackMBB, DL, TII, MorestackName)
: BuildMI(MorestackMBB, DL, TII.get(AArch64::BL))
.addExternalSymbol(MorestackName);
: BuildMI(MorestackMBB, DL, TII.get(AArch64::BL));
if (!UseStackGrowthStatepoint)
goabi::addGoObjABI0Callee(Morestack, MF, MorestackName);
Morestack.addReg(AArch64::X3, RegState::Implicit);
if (HasClosureContext)
Morestack.addReg(AArch64::X26, RegState::Implicit);
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/X86/X86ExpandPseudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ bool X86ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
if (MF.getTarget().getTargetTriple().isOSBinFormatGoObj()) {
MCContext &Ctx = MF.getContext();
MCSymbol *Callee = Ctx.getOrCreateSymbol(WriteBarrierName);
Ctx.setGoObjSymbolABI(Callee, GoObj::SymABIInternal);
Call.addSym(Callee);
} else {
Call.addExternalSymbol(WriteBarrierName);
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/Target/X86/X86FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ static MachineInstrBuilder buildGoStackGrowthStatepoint(MachineFunction &MF,
MachineBasicBlock &MBB,
const DebugLoc &DL,
const X86InstrInfo &TII,
const char *Callee) {
StringRef CalleeName) {
MachineInstrBuilder Statepoint =
BuildMI(&MBB, DL, TII.get(TargetOpcode::STATEPOINT))
.addImm(goabi::StackGrowthStatepointID)
.addImm(0)
.addImm(0)
.addExternalSymbol(Callee);
.addImm(0);
goabi::addGoObjABI0Callee(Statepoint, MF, CalleeName);
auto AddConstant = [&](uint64_t Value) {
Statepoint.addImm(StackMaps::ConstantOp).addImm(Value);
};
Expand Down Expand Up @@ -421,8 +421,9 @@ static void emitGoStackCheck(MachineFunction &MF,
UseStackGrowthStatepoint
? buildGoStackGrowthStatepoint(MF, *MorestackMBB, DL, TII,
MorestackName)
: BuildMI(MorestackMBB, DL, TII.get(X86::CALL64pcrel32))
.addExternalSymbol(MorestackName);
: BuildMI(MorestackMBB, DL, TII.get(X86::CALL64pcrel32));
if (!UseStackGrowthStatepoint)
goabi::addGoObjABI0Callee(Morestack, MF, MorestackName);
if (HasClosureContext)
Morestack.addReg(X86::RDX, RegState::Implicit);
emitGoRegSpills(MF, *MorestackMBB, Homes, /*Reload=*/true);
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/CodeGen/AArch64/goobj-abi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ entry:
ret i64 %sum
}

define goabi0 i64 @stackadd(i64 %a, i64 %b) #0 {
define goabi0 i64 @"stackadd<ABI0>"(i64 %a, i64 %b) #0 {
entry:
%sum = add i64 %a, %b
ret i64 %sum
Expand Down Expand Up @@ -78,7 +78,7 @@ entry:
ret %aggregate %result
}

; ASM-LABEL: stackadd:
; ASM-LABEL: "stackadd<ABI0>":
; ASM: ldp x{{[0-9]+}}, x{{[0-9]+}}, [sp, #8]
; ASM: str x{{[0-9]+}}, [sp, #24]

Expand All @@ -88,7 +88,7 @@ entry:
; ASM: cmp sp, x17
; ASM: b.hi [[CALLS_BODY:.LBB[0-9_]+]]
; ASM: mov x3, x30
; ASM: bl runtime.morestack_noctxt
; ASM: bl "runtime.morestack_noctxt<ABI0>"
; ASM-NEXT: b [[CALLS_CHECK]]
; ASM: [[CALLS_BODY]]:
; ASM: str x30, [sp, #-16]!
Expand Down Expand Up @@ -126,7 +126,7 @@ entry:
; ASM: [[LARGE_MORESTACK]]:
; ASM: mov x3, x30
; ASM: str x0, [sp, #8]
; ASM: bl runtime.morestack_noctxt
; ASM: bl "runtime.morestack_noctxt<ABI0>"
; ASM: ldr x0, [sp, #8]
; ASM-NEXT: b [[LARGE_CHECK]]
; ASM: [[LARGE_BODY]]:
Expand All @@ -145,7 +145,7 @@ entry:
; ASM: b.hi [[CLOSURE_BODY:.LBB[0-9_]+]]
; ASM: [[CLOSURE_MORESTACK]]:
; ASM: mov x3, x30
; ASM: bl runtime.morestack
; ASM: bl "runtime.morestack<ABI0>"
; ASM: b [[CLOSURE_CHECK]]
; ASM: [[CLOSURE_BODY]]:

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AArch64/goobj-ir-config.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
; settings in the IR. llc must not need a matching set of -goobj-* flags.
target triple = "aarch64-apple-darwin-goobj"

define void @main.main() {
define goabiinternal void @main.main() {
entry:
ret void
}
Expand Down
Loading
Loading