LLVM 20.0.0git
loongarch.h
Go to the documentation of this file.
1//= loongarch.h - Generic JITLink loongarch edge kinds, utilities -*- C++ -*-=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Generic utilities for graphs representing loongarch objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
14#define LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
15
16#include "TableManager.h"
19
20namespace llvm {
21namespace jitlink {
22namespace loongarch {
23
24/// Represents loongarch fixups.
26 /// A plain 64-bit pointer value relocation.
27 ///
28 /// Fixup expression:
29 /// Fixup <- Target + Addend : uint64
30 ///
32
33 /// A plain 32-bit pointer value relocation.
34 ///
35 /// Fixup expression:
36 /// Fixup <- Target + Addend : uint32
37 ///
38 /// Errors:
39 /// - The target must reside in the low 32-bits of the address space,
40 /// otherwise an out-of-range error will be returned.
41 ///
43
44 /// A 26-bit PC-relative branch.
45 ///
46 /// Represents a PC-relative call or branch to a target within +/-128Mb. The
47 /// target must be 4-byte aligned.
48 ///
49 /// Fixup expression:
50 /// Fixup <- (Target - Fixup + Addend) >> 2 : int26
51 ///
52 /// Notes:
53 /// The '26' in the name refers to the number operand bits and follows the
54 /// naming convention used by the corresponding ELF relocations. Since the low
55 /// two bits must be zero (because of the 4-byte alignment of the target) the
56 /// operand is effectively a signed 28-bit number.
57 ///
58 /// Errors:
59 /// - The result of the unshifted part of the fixup expression must be
60 /// 4-byte aligned otherwise an alignment error will be returned.
61 /// - The result of the fixup expression must fit into an int26 otherwise an
62 /// out-of-range error will be returned.
63 ///
65
66 /// A 32-bit delta.
67 ///
68 /// Delta from the fixup to the target.
69 ///
70 /// Fixup expression:
71 /// Fixup <- Target - Fixup + Addend : int32
72 ///
73 /// Errors:
74 /// - The result of the fixup expression must fit into an int32, otherwise
75 /// an out-of-range error will be returned.
76 ///
78
79 /// A 32-bit negative delta.
80 ///
81 /// Delta from the target back to the fixup.
82 ///
83 /// Fixup expression:
84 /// Fixup <- Fixup - Target + Addend : int32
85 ///
86 /// Errors:
87 /// - The result of the fixup expression must fit into an int32, otherwise
88 /// an out-of-range error will be returned.
89 ///
91
92 /// A 64-bit delta.
93 ///
94 /// Delta from the fixup to the target.
95 ///
96 /// Fixup expression:
97 /// Fixup <- Target - Fixup + Addend : int64
98 ///
100
101 /// The signed 20-bit delta from the fixup page to the page containing the
102 /// target.
103 ///
104 /// Fixup expression:
105 /// Fixup <- (((Target + Addend + ((Target + Addend) & 0x800)) & ~0xfff)
106 // - (Fixup & ~0xfff)) >> 12 : int20
107 ///
108 /// Notes:
109 /// For PCALAU12I fixups.
110 ///
111 /// Errors:
112 /// - The result of the fixup expression must fit into an int20 otherwise an
113 /// out-of-range error will be returned.
114 ///
116
117 /// The 12-bit offset of the target within its page.
118 ///
119 /// Typically used to fix up ADDI/LD_W/LD_D immediates.
120 ///
121 /// Fixup expression:
122 /// Fixup <- ((Target + Addend) >> Shift) & 0xfff : int12
123 ///
125
126 /// A GOT entry getter/constructor, transformed to Page20 pointing at the GOT
127 /// entry for the original target.
128 ///
129 /// Indicates that this edge should be transformed into a Page20 targeting
130 /// the GOT entry for the edge's current target, maintaining the same addend.
131 /// A GOT entry for the target should be created if one does not already
132 /// exist.
133 ///
134 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
135 /// by default.
136 ///
137 /// Fixup expression:
138 /// NONE
139 ///
140 /// Errors:
141 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
142 /// phase will result in an assert/unreachable during the fixup phase.
143 ///
145
146 /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
147 /// the GOT entry for the original target.
148 ///
149 /// Indicates that this edge should be transformed into a PageOffset12
150 /// targeting the GOT entry for the edge's current target, maintaining the
151 /// same addend. A GOT entry for the target should be created if one does not
152 /// already exist.
153 ///
154 /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
155 /// by default.
156 ///
157 /// Fixup expression:
158 /// NONE
159 ///
161
162 /// A 36-bit PC-relative call.
163 ///
164 /// Represents a PC-relative call to a target within [-128G - 0x20000, +128G
165 /// - 0x20000). The target must be 4-byte aligned. For adjacent pcaddu18i+jirl
166 /// instruction pairs.
167 ///
168 /// Fixup expression:
169 /// Fixup <- (Target - Fixup + Addend) >> 2 : int36
170 ///
171 /// Notes:
172 /// The '36' in the name refers to the number operand bits and follows the
173 /// naming convention used by the corresponding ELF relocations. Since the low
174 /// two bits must be zero (because of the 4-byte alignment of the target) the
175 /// operand is effectively a signed 38-bit number.
176 ///
177 /// Errors:
178 /// - The result of the unshifted part of the fixup expression must be
179 /// 4-byte aligned otherwise an alignment error will be returned.
180 /// - The result of the fixup expression must fit into an int36 otherwise an
181 /// out-of-range error will be returned.
182 ///
184};
185
186/// Returns a string name for the given loongarch edge. For debugging purposes
187/// only.
188const char *getEdgeKindName(Edge::Kind K);
189
190// Returns extract bits Val[Hi:Lo].
191inline uint32_t extractBits(uint64_t Val, unsigned Hi, unsigned Lo) {
192 return Hi == 63 ? Val >> Lo : (Val & (((1UL << (Hi + 1)) - 1))) >> Lo;
193}
194
195/// Apply fixup expression for edge to block content.
196inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E) {
197 using namespace support;
198
199 char *BlockWorkingMem = B.getAlreadyMutableContent().data();
200 char *FixupPtr = BlockWorkingMem + E.getOffset();
201 uint64_t FixupAddress = (B.getAddress() + E.getOffset()).getValue();
202 uint64_t TargetAddress = E.getTarget().getAddress().getValue();
203 int64_t Addend = E.getAddend();
204
205 switch (E.getKind()) {
206 case Pointer64:
207 *(ulittle64_t *)FixupPtr = TargetAddress + Addend;
208 break;
209 case Pointer32: {
210 uint64_t Value = TargetAddress + Addend;
211 if (Value > std::numeric_limits<uint32_t>::max())
212 return makeTargetOutOfRangeError(G, B, E);
213 *(ulittle32_t *)FixupPtr = Value;
214 break;
215 }
216 case Branch26PCRel: {
217 int64_t Value = TargetAddress - FixupAddress + Addend;
218
219 if (!isInt<28>(Value))
220 return makeTargetOutOfRangeError(G, B, E);
221
222 if (!isShiftedInt<26, 2>(Value))
223 return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
224
225 uint32_t RawInstr = *(little32_t *)FixupPtr;
226 uint32_t Imm = static_cast<uint32_t>(Value >> 2);
227 uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
228 uint32_t Imm25_16 = extractBits(Imm, /*Hi=*/25, /*Lo=*/16);
229 *(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm25_16;
230 break;
231 }
232 case Delta32: {
233 int64_t Value = TargetAddress - FixupAddress + Addend;
234
235 if (!isInt<32>(Value))
236 return makeTargetOutOfRangeError(G, B, E);
237 *(little32_t *)FixupPtr = Value;
238 break;
239 }
240 case NegDelta32: {
241 int64_t Value = FixupAddress - TargetAddress + Addend;
242 if (!isInt<32>(Value))
243 return makeTargetOutOfRangeError(G, B, E);
244 *(little32_t *)FixupPtr = Value;
245 break;
246 }
247 case Delta64:
248 *(little64_t *)FixupPtr = TargetAddress - FixupAddress + Addend;
249 break;
250 case Page20: {
251 uint64_t Target = TargetAddress + Addend;
252 uint64_t TargetPage =
253 (Target + (Target & 0x800)) & ~static_cast<uint64_t>(0xfff);
254 uint64_t PCPage = FixupAddress & ~static_cast<uint64_t>(0xfff);
255
256 int64_t PageDelta = TargetPage - PCPage;
257 if (!isInt<32>(PageDelta))
258 return makeTargetOutOfRangeError(G, B, E);
259
260 uint32_t RawInstr = *(little32_t *)FixupPtr;
261 uint32_t Imm31_12 = extractBits(PageDelta, /*Hi=*/31, /*Lo=*/12) << 5;
262 *(little32_t *)FixupPtr = RawInstr | Imm31_12;
263 break;
264 }
265 case PageOffset12: {
266 uint64_t TargetOffset = (TargetAddress + Addend) & 0xfff;
267
268 uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
269 uint32_t Imm11_0 = TargetOffset << 10;
270 *(ulittle32_t *)FixupPtr = RawInstr | Imm11_0;
271 break;
272 }
273 case Call36PCRel: {
274 int64_t Value = TargetAddress - FixupAddress + Addend;
275
276 if ((Value + 0x20000) != llvm::SignExtend64(Value + 0x20000, 38))
277 return makeTargetOutOfRangeError(G, B, E);
278
279 if (!isShiftedInt<36, 2>(Value))
280 return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
281
282 uint32_t Pcaddu18i = *(little32_t *)FixupPtr;
283 uint32_t Hi20 = extractBits(Value + (1 << 17), /*Hi=*/37, /*Lo=*/18) << 5;
284 *(little32_t *)FixupPtr = Pcaddu18i | Hi20;
285 uint32_t Jirl = *(little32_t *)(FixupPtr + 4);
286 uint32_t Lo16 = extractBits(Value, /*Hi=*/17, /*Lo=*/2) << 10;
287 *(little32_t *)(FixupPtr + 4) = Jirl | Lo16;
288 break;
289 }
290 default:
291 return make_error<JITLinkError>(
292 "In graph " + G.getName() + ", section " + B.getSection().getName() +
293 " unsupported edge kind " + getEdgeKindName(E.getKind()));
294 }
295
296 return Error::success();
297}
298
299/// loongarch null pointer content.
300extern const char NullPointerContent[8];
302 return {reinterpret_cast<const char *>(NullPointerContent),
303 G.getPointerSize()};
304}
305
306/// loongarch stub content.
307///
308/// Contains the instruction sequence for an indirect jump via an in-memory
309/// pointer:
310/// pcalau12i $t8, %page20(ptr)
311/// ld.[w/d] $t8, %pageoff12(ptr)
312/// jr $t8
313constexpr size_t StubEntrySize = 12;
317 auto StubContent =
318 G.getPointerSize() == 8 ? LA64StubContent : LA32StubContent;
319 return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
320}
321
322/// Creates a new pointer block in the given section and returns an
323/// Anonymous symbol pointing to it.
324///
325/// If InitialTarget is given then an Pointer64 relocation will be added to the
326/// block pointing at InitialTarget.
327///
328/// The pointer block will have the following default values:
329/// alignment: PointerSize
330/// alignment-offset: 0
332 Symbol *InitialTarget = nullptr,
333 uint64_t InitialAddend = 0) {
334 auto &B = G.createContentBlock(PointerSection, getGOTEntryBlockContent(G),
335 orc::ExecutorAddr(), G.getPointerSize(), 0);
336 if (InitialTarget)
337 B.addEdge(G.getPointerSize() == 8 ? Pointer64 : Pointer32, 0,
338 *InitialTarget, InitialAddend);
339 return G.addAnonymousSymbol(B, 0, G.getPointerSize(), false, false);
340}
341
342/// Create a jump stub that jumps via the pointer at the given symbol and
343/// an anonymous symbol pointing to it. Return the anonymous symbol.
345 Section &StubSection,
346 Symbol &PointerSymbol) {
347 Block &StubContentBlock = G.createContentBlock(
348 StubSection, getStubBlockContent(G), orc::ExecutorAddr(), 4, 0);
349 StubContentBlock.addEdge(Page20, 0, PointerSymbol, 0);
350 StubContentBlock.addEdge(PageOffset12, 4, PointerSymbol, 0);
351 return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, false);
352}
353
354/// Global Offset Table Builder.
355class GOTTableManager : public TableManager<GOTTableManager> {
356public:
357 static StringRef getSectionName() { return "$__GOT"; }
358
360 Edge::Kind KindToSet = Edge::Invalid;
361 switch (E.getKind()) {
363 KindToSet = Page20;
364 break;
366 KindToSet = PageOffset12;
367 break;
368 default:
369 return false;
370 }
371 assert(KindToSet != Edge::Invalid &&
372 "Fell through switch, but no new kind to set");
373 DEBUG_WITH_TYPE("jitlink", {
374 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
375 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
376 << formatv("{0:x}", E.getOffset()) << ")\n";
377 });
378 E.setKind(KindToSet);
379 E.setTarget(getEntryForTarget(G, E.getTarget()));
380 return true;
381 }
382
384 return createAnonymousPointer(G, getGOTSection(G), &Target);
385 }
386
387private:
388 Section &getGOTSection(LinkGraph &G) {
389 if (!GOTSection)
390 GOTSection = &G.createSection(getSectionName(),
392 return *GOTSection;
393 }
394
395 Section *GOTSection = nullptr;
396};
397
398/// Procedure Linkage Table Builder.
399class PLTTableManager : public TableManager<PLTTableManager> {
400public:
402
403 static StringRef getSectionName() { return "$__STUBS"; }
404
406 if ((E.getKind() == Branch26PCRel || E.getKind() == Call36PCRel) &&
407 !E.getTarget().isDefined()) {
408 DEBUG_WITH_TYPE("jitlink", {
409 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
410 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
411 << formatv("{0:x}", E.getOffset()) << ")\n";
412 });
413 E.setTarget(getEntryForTarget(G, E.getTarget()));
414 return true;
415 }
416 return false;
417 }
418
422 }
423
424public:
426 if (!StubsSection)
427 StubsSection = &G.createSection(getSectionName(),
429 return *StubsSection;
430 }
431
434};
435
436} // namespace loongarch
437} // namespace jitlink
438} // namespace llvm
439
440#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
Represents an address in the executor process.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:581