Skip to content

Commit d5eb0a9

Browse files
committed
[JITLink][i386] Avoid 'i386' name clashing with built-in macro
When compiling llvm on an actual i386 platform, both clang and gcc define a built-in macro `i386` to the value 1. This clashes with the `llvm::jitlink::i386` namespace: ``` In file included from llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp:18: llvm/include/llvm/ExecutionEngine/JITLink/i386.h:19:24: error: expected '{' 19 | namespace llvm::jitlink::i386 { | ^ llvm/include/llvm/ExecutionEngine/JITLink/i386.h:19:26: error: expected unqualified-id 19 | namespace llvm::jitlink::i386 { | ^ ``` The macro name 'i386' is obviously a historical bad choice, but since it existed long before llvm, llvm should either rename its namespace, or actively undefine the macro in `i386.h` (similar to e.g. https://github.com/google/swiftshader/blob/master/third_party/llvm-16.0/Android.bp#L1510). This particular pull request implements the rename, but is meant to gauge opinions on how to solve this issue.
1 parent 0fdb908 commit d5eb0a9

File tree

5 files changed

+48
-48
lines changed

5 files changed

+48
-48
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/i386.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
1717
#include "llvm/ExecutionEngine/JITLink/TableManager.h"
1818

19-
namespace llvm::jitlink::i386 {
19+
namespace llvm::jitlink::i386_ {
2020
/// Represets i386 fixups
2121
enum EdgeKind_i386 : Edge::Kind {
2222

@@ -184,31 +184,31 @@ const char *getEdgeKindName(Edge::Kind K);
184184
/// Apply fixup expression for edge to block content.
185185
inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
186186
const Symbol *GOTSymbol) {
187-
using namespace i386;
187+
using namespace i386_;
188188
using namespace llvm::support;
189189

190190
char *BlockWorkingMem = B.getAlreadyMutableContent().data();
191191
char *FixupPtr = BlockWorkingMem + E.getOffset();
192192
auto FixupAddress = B.getAddress() + E.getOffset();
193193

194194
switch (E.getKind()) {
195-
case i386::None: {
195+
case i386_::None: {
196196
break;
197197
}
198198

199-
case i386::Pointer32: {
199+
case i386_::Pointer32: {
200200
uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
201201
*(ulittle32_t *)FixupPtr = Value;
202202
break;
203203
}
204204

205-
case i386::PCRel32: {
205+
case i386_::PCRel32: {
206206
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
207207
*(little32_t *)FixupPtr = Value;
208208
break;
209209
}
210210

211-
case i386::Pointer16: {
211+
case i386_::Pointer16: {
212212
uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
213213
if (LLVM_LIKELY(isUInt<16>(Value)))
214214
*(ulittle16_t *)FixupPtr = Value;
@@ -217,7 +217,7 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
217217
break;
218218
}
219219

220-
case i386::PCRel16: {
220+
case i386_::PCRel16: {
221221
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
222222
if (LLVM_LIKELY(isInt<16>(Value)))
223223
*(little16_t *)FixupPtr = Value;
@@ -226,23 +226,23 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
226226
break;
227227
}
228228

229-
case i386::Delta32: {
229+
case i386_::Delta32: {
230230
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
231231
*(little32_t *)FixupPtr = Value;
232232
break;
233233
}
234234

235-
case i386::Delta32FromGOT: {
235+
case i386_::Delta32FromGOT: {
236236
assert(GOTSymbol && "No GOT section symbol");
237237
int32_t Value =
238238
E.getTarget().getAddress() - GOTSymbol->getAddress() + E.getAddend();
239239
*(little32_t *)FixupPtr = Value;
240240
break;
241241
}
242242

243-
case i386::BranchPCRel32:
244-
case i386::BranchPCRel32ToPtrJumpStub:
245-
case i386::BranchPCRel32ToPtrJumpStubBypassable: {
243+
case i386_::BranchPCRel32:
244+
case i386_::BranchPCRel32ToPtrJumpStub:
245+
case i386_::BranchPCRel32ToPtrJumpStubBypassable: {
246246
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
247247
*(little32_t *)FixupPtr = Value;
248248
break;
@@ -328,14 +328,14 @@ class GOTTableManager : public TableManager<GOTTableManager> {
328328
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
329329
Edge::Kind KindToSet = Edge::Invalid;
330330
switch (E.getKind()) {
331-
case i386::Delta32FromGOT: {
331+
case i386_::Delta32FromGOT: {
332332
// we need to make sure that the GOT section exists, but don't otherwise
333333
// need to fix up this edge
334334
getGOTSection(G);
335335
return false;
336336
}
337-
case i386::RequestGOTAndTransformToDelta32FromGOT:
338-
KindToSet = i386::Delta32FromGOT;
337+
case i386_::RequestGOTAndTransformToDelta32FromGOT:
338+
KindToSet = i386_::Delta32FromGOT;
339339
break;
340340
default:
341341
return false;
@@ -374,15 +374,15 @@ class PLTTableManager : public TableManager<PLTTableManager> {
374374
static StringRef getSectionName() { return "$__STUBS"; }
375375

376376
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
377-
if (E.getKind() == i386::BranchPCRel32 && !E.getTarget().isDefined()) {
377+
if (E.getKind() == i386_::BranchPCRel32 && !E.getTarget().isDefined()) {
378378
DEBUG_WITH_TYPE("jitlink", {
379379
dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
380380
<< B->getFixupAddress(E) << " (" << B->getAddress() << " + "
381381
<< formatv("{0:x}", E.getOffset()) << ")\n";
382382
});
383383
// Set the edge kind to Branch32ToPtrJumpStubBypassable to enable it to
384384
// be optimized when the target is in-range.
385-
E.setKind(i386::BranchPCRel32ToPtrJumpStubBypassable);
385+
E.setKind(i386_::BranchPCRel32ToPtrJumpStubBypassable);
386386
E.setTarget(getEntryForTarget(G, E.getTarget()));
387387
return true;
388388
}
@@ -414,6 +414,6 @@ class PLTTableManager : public TableManager<PLTTableManager> {
414414
/// target
415415
Error optimizeGOTAndStubAccesses(LinkGraph &G);
416416

417-
} // namespace llvm::jitlink::i386
417+
} // namespace llvm::jitlink::i386_
418418

419419
#endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H

llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ constexpr StringRef ELFGOTSymbolName = "_GLOBAL_OFFSET_TABLE_";
2929
Error buildTables_ELF_i386(LinkGraph &G) {
3030
LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
3131

32-
i386::GOTTableManager GOT;
33-
i386::PLTTableManager PLT(GOT);
32+
i386_::GOTTableManager GOT;
33+
i386_::PLTTableManager PLT(GOT);
3434
visitExistingEdges(G, GOT, PLT);
3535
return Error::success();
3636
}
@@ -59,7 +59,7 @@ class ELFJITLinker_i386 : public JITLinker<ELFJITLinker_i386> {
5959
if (Sym.getName() != nullptr &&
6060
*Sym.getName() == ELFGOTSymbolName)
6161
if (auto *GOTSection = G.findSectionByName(
62-
i386::GOTTableManager::getSectionName())) {
62+
i386_::GOTTableManager::getSectionName())) {
6363
GOTSymbol = &Sym;
6464
return {*GOTSection, true};
6565
}
@@ -79,7 +79,7 @@ class ELFJITLinker_i386 : public JITLinker<ELFJITLinker_i386> {
7979
// record it, otherwise we'll create our own.
8080
// If there's a GOT section but we didn't find an external GOT symbol...
8181
if (auto *GOTSection =
82-
G.findSectionByName(i386::GOTTableManager::getSectionName())) {
82+
G.findSectionByName(i386_::GOTTableManager::getSectionName())) {
8383

8484
// Check for an existing defined symbol.
8585
for (auto *Sym : GOTSection->symbols())
@@ -106,15 +106,15 @@ class ELFJITLinker_i386 : public JITLinker<ELFJITLinker_i386> {
106106
}
107107

108108
Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
109-
return i386::applyFixup(G, B, E, GOTSymbol);
109+
return i386_::applyFixup(G, B, E, GOTSymbol);
110110
}
111111
};
112112

113113
template <typename ELFT>
114114
class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
115115
private:
116-
static Expected<i386::EdgeKind_i386> getRelocationKind(const uint32_t Type) {
117-
using namespace i386;
116+
static Expected<i386_::EdgeKind_i386> getRelocationKind(const uint32_t Type) {
117+
using namespace i386_;
118118
switch (Type) {
119119
case ELF::R_386_NONE:
120120
return EdgeKind_i386::None;
@@ -179,31 +179,31 @@ class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
179179
Base::GraphSymbols.size()),
180180
inconvertibleErrorCode());
181181

182-
Expected<i386::EdgeKind_i386> Kind = getRelocationKind(Rel.getType(false));
182+
Expected<i386_::EdgeKind_i386> Kind = getRelocationKind(Rel.getType(false));
183183
if (!Kind)
184184
return Kind.takeError();
185185

186186
auto FixupAddress = orc::ExecutorAddr(FixupSection.sh_addr) + Rel.r_offset;
187187
int64_t Addend = 0;
188188

189189
switch (*Kind) {
190-
case i386::EdgeKind_i386::None:
190+
case i386_::EdgeKind_i386::None:
191191
break;
192-
case i386::EdgeKind_i386::Pointer32:
193-
case i386::EdgeKind_i386::PCRel32:
194-
case i386::EdgeKind_i386::RequestGOTAndTransformToDelta32FromGOT:
195-
case i386::EdgeKind_i386::Delta32:
196-
case i386::EdgeKind_i386::Delta32FromGOT:
197-
case i386::EdgeKind_i386::BranchPCRel32:
198-
case i386::EdgeKind_i386::BranchPCRel32ToPtrJumpStub:
199-
case i386::EdgeKind_i386::BranchPCRel32ToPtrJumpStubBypassable: {
192+
case i386_::EdgeKind_i386::Pointer32:
193+
case i386_::EdgeKind_i386::PCRel32:
194+
case i386_::EdgeKind_i386::RequestGOTAndTransformToDelta32FromGOT:
195+
case i386_::EdgeKind_i386::Delta32:
196+
case i386_::EdgeKind_i386::Delta32FromGOT:
197+
case i386_::EdgeKind_i386::BranchPCRel32:
198+
case i386_::EdgeKind_i386::BranchPCRel32ToPtrJumpStub:
199+
case i386_::EdgeKind_i386::BranchPCRel32ToPtrJumpStubBypassable: {
200200
const char *FixupContent = BlockToFix.getContent().data() +
201201
(FixupAddress - BlockToFix.getAddress());
202202
Addend = *(const support::little32_t *)FixupContent;
203203
break;
204204
}
205-
case i386::EdgeKind_i386::Pointer16:
206-
case i386::EdgeKind_i386::PCRel16: {
205+
case i386_::EdgeKind_i386::Pointer16:
206+
case i386_::EdgeKind_i386::PCRel16: {
207207
const char *FixupContent = BlockToFix.getContent().data() +
208208
(FixupAddress - BlockToFix.getAddress());
209209
Addend = *(const support::little16_t *)FixupContent;
@@ -215,7 +215,7 @@ class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
215215
Edge GE(*Kind, Offset, *GraphSymbol, Addend);
216216
LLVM_DEBUG({
217217
dbgs() << " ";
218-
printEdge(dbgs(), BlockToFix, GE, i386::getEdgeKindName(*Kind));
218+
printEdge(dbgs(), BlockToFix, GE, i386_::getEdgeKindName(*Kind));
219219
dbgs() << "\n";
220220
});
221221

@@ -229,7 +229,7 @@ class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
229229
Triple TT, SubtargetFeatures Features)
230230
: ELFLinkGraphBuilder<ELFT>(Obj, std::move(SSP), std::move(TT),
231231
std::move(Features), FileName,
232-
i386::getEdgeKindName) {}
232+
i386_::getEdgeKindName) {}
233233
};
234234

235235
Expected<std::unique_ptr<LinkGraph>>
@@ -273,7 +273,7 @@ void link_ELF_i386(std::unique_ptr<LinkGraph> G,
273273
Config.PostPrunePasses.push_back(buildTables_ELF_i386);
274274

275275
// Add GOT/Stubs optimizer pass.
276-
Config.PreFixupPasses.push_back(i386::optimizeGOTAndStubAccesses);
276+
Config.PreFixupPasses.push_back(i386_::optimizeGOTAndStubAccesses);
277277
}
278278
if (auto Err = Ctx->modifyPassConfig(*G, Config))
279279
return Ctx->notifyFailed(std::move(Err));

llvm/lib/ExecutionEngine/JITLink/JITLink.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ AnonymousPointerCreator getAnonymousPointerCreator(const Triple &TT) {
466466
case Triple::x86_64:
467467
return x86_64::createAnonymousPointer;
468468
case Triple::x86:
469-
return i386::createAnonymousPointer;
469+
return i386_::createAnonymousPointer;
470470
case Triple::loongarch32:
471471
case Triple::loongarch64:
472472
return loongarch::createAnonymousPointer;
@@ -482,7 +482,7 @@ PointerJumpStubCreator getPointerJumpStubCreator(const Triple &TT) {
482482
case Triple::x86_64:
483483
return x86_64::createAnonymousPointerJumpStub;
484484
case Triple::x86:
485-
return i386::createAnonymousPointerJumpStub;
485+
return i386_::createAnonymousPointerJumpStub;
486486
case Triple::loongarch32:
487487
case Triple::loongarch64:
488488
return loongarch::createAnonymousPointerJumpStub;

llvm/lib/ExecutionEngine/JITLink/i386.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#define DEBUG_TYPE "jitlink"
1616

17-
namespace llvm::jitlink::i386 {
17+
namespace llvm::jitlink::i386_ {
1818

1919
const char *getEdgeKindName(Edge::Kind K) {
2020
switch (K) {
@@ -55,7 +55,7 @@ Error optimizeGOTAndStubAccesses(LinkGraph &G) {
5555

5656
for (auto *B : G.blocks())
5757
for (auto &E : B->edges()) {
58-
if (E.getKind() == i386::BranchPCRel32ToPtrJumpStubBypassable) {
58+
if (E.getKind() == i386_::BranchPCRel32ToPtrJumpStubBypassable) {
5959
auto &StubBlock = E.getTarget().getBlock();
6060
assert(StubBlock.getSize() == sizeof(PointerJumpStubContent) &&
6161
"Stub block should be stub sized");
@@ -74,7 +74,7 @@ Error optimizeGOTAndStubAccesses(LinkGraph &G) {
7474

7575
int64_t Displacement = TargetAddr - EdgeAddr + 4;
7676
if (isInt<32>(Displacement)) {
77-
E.setKind(i386::BranchPCRel32);
77+
E.setKind(i386_::BranchPCRel32);
7878
E.setTarget(GOTTarget);
7979
LLVM_DEBUG({
8080
dbgs() << " Replaced stub branch with direct branch:\n ";
@@ -88,4 +88,4 @@ Error optimizeGOTAndStubAccesses(LinkGraph &G) {
8888
return Error::success();
8989
}
9090

91-
} // namespace llvm::jitlink::i386
91+
} // namespace llvm::jitlink::i386_

llvm/unittests/ExecutionEngine/JITLink/StubsTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ TEST(StubsTest, StubsGeneration_i386) {
102102
LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
103103
Triple("i386-unknown-linux-gnu"), SubtargetFeatures(),
104104
getGenericEdgeKindName);
105-
auto [PointerSym, StubSym] = GenerateStub(G, 4U, i386::Pointer32);
105+
auto [PointerSym, StubSym] = GenerateStub(G, 4U, i386_::Pointer32);
106106

107107
EXPECT_EQ(std::distance(StubSym.getBlock().edges().begin(),
108108
StubSym.getBlock().edges().end()),
109109
1U);
110110
auto &JumpEdge = *StubSym.getBlock().edges().begin();
111-
EXPECT_EQ(JumpEdge.getKind(), i386::Pointer32);
111+
EXPECT_EQ(JumpEdge.getKind(), i386_::Pointer32);
112112
EXPECT_EQ(&JumpEdge.getTarget(), &PointerSym);
113113
EXPECT_EQ(StubSym.getBlock().getContent(),
114114
ArrayRef<char>(PointerJumpStubContent));

0 commit comments

Comments
 (0)