Skip to content

Commit a847b66

Browse files
jonathonpenixtru
authored andcommitted
[RISCV] Don't outline pcrel_lo when the function has a section prefix (#107943)
GNU ld will error when encountering a pcrel_lo whose corresponding pcrel_hi is in a different section. [1] introduced a check to help prevent this issue by preventing outlining in a few circumstances. However, we can also hit this same issue when outlining from functions with prefixes ("hot"/"unlikely"/"unknown" from profile information, for example) as the outlined function might not have the same prefix, possibly resulting in a "paired" pcrel_lo and pcrel_hi ending up in different sections. To prevent this issue, take a similar approach as [1] and additionally prevent outlining when we see a pcrel_lo and the function has a prefix. [1] 96c85f8 Fixes #107520 (cherry picked from commit 866b93e)
1 parent 6278084 commit a847b66

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2902,7 +2902,7 @@ RISCVInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
29022902
// if any possible.
29032903
if (MO.getTargetFlags() == RISCVII::MO_PCREL_LO &&
29042904
(MI.getMF()->getTarget().getFunctionSections() || F.hasComdat() ||
2905-
F.hasSection()))
2905+
F.hasSection() || F.getSectionPrefix()))
29062906
return outliner::InstrType::Illegal;
29072907
}
29082908

llvm/test/CodeGen/RISCV/machineoutliner-pcrel-lo.mir

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
define i32 @foo2(i32 %a, i32 %b) comdat { ret i32 0 }
1919

2020
define i32 @foo3(i32 %a, i32 %b) section ".abc" { ret i32 0 }
21+
22+
define i32 @foo4(i32 %a, i32 %b) !section_prefix !0 { ret i32 0 }
23+
!0 = !{!"function_section_prefix", !"myprefix"}
2124
...
2225
---
2326
name: foo
@@ -27,23 +30,24 @@ body: |
2730
; CHECK: bb.0:
2831
; CHECK-NEXT: liveins: $x10, $x11, $x13
2932
; CHECK-NEXT: {{ $}}
30-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
33+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
3134
; CHECK-NEXT: PseudoBR %bb.3
3235
; CHECK-NEXT: {{ $}}
3336
; CHECK-NEXT: bb.1:
3437
; CHECK-NEXT: liveins: $x10, $x11, $x13
3538
; CHECK-NEXT: {{ $}}
36-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
39+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
3740
; CHECK-NEXT: PseudoBR %bb.3
3841
; CHECK-NEXT: {{ $}}
3942
; CHECK-NEXT: bb.2:
4043
; CHECK-NEXT: liveins: $x10, $x11, $x13
4144
; CHECK-NEXT: {{ $}}
42-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
45+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11, implicit $x13
4346
; CHECK-NEXT: PseudoBR %bb.3
4447
; CHECK-NEXT: {{ $}}
4548
; CHECK-NEXT: bb.3:
4649
; CHECK-NEXT: PseudoRET
50+
;
4751
; CHECK-FS-LABEL: name: foo
4852
; CHECK-FS: bb.0:
4953
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
@@ -109,26 +113,27 @@ body: |
109113
; CHECK: bb.0:
110114
; CHECK-NEXT: liveins: $x10, $x11, $x13
111115
; CHECK-NEXT: {{ $}}
112-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
116+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
113117
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
114118
; CHECK-NEXT: PseudoBR %bb.3
115119
; CHECK-NEXT: {{ $}}
116120
; CHECK-NEXT: bb.1:
117121
; CHECK-NEXT: liveins: $x10, $x11, $x13
118122
; CHECK-NEXT: {{ $}}
119-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
123+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
120124
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
121125
; CHECK-NEXT: PseudoBR %bb.3
122126
; CHECK-NEXT: {{ $}}
123127
; CHECK-NEXT: bb.2:
124128
; CHECK-NEXT: liveins: $x10, $x11, $x13
125129
; CHECK-NEXT: {{ $}}
126-
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_1, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
130+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
127131
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
128132
; CHECK-NEXT: PseudoBR %bb.3
129133
; CHECK-NEXT: {{ $}}
130134
; CHECK-NEXT: bb.3:
131135
; CHECK-NEXT: PseudoRET
136+
;
132137
; CHECK-FS-LABEL: name: foo2
133138
; CHECK-FS: bb.0:
134139
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
@@ -223,6 +228,7 @@ body: |
223228
; CHECK-NEXT: {{ $}}
224229
; CHECK-NEXT: bb.3:
225230
; CHECK-NEXT: PseudoRET
231+
;
226232
; CHECK-FS-LABEL: name: foo3
227233
; CHECK-FS: bb.0:
228234
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
@@ -289,3 +295,89 @@ body: |
289295
bb.3:
290296
PseudoRET
291297
...
298+
---
299+
name: foo4
300+
tracksRegLiveness: true
301+
body: |
302+
; CHECK-LABEL: name: foo4
303+
; CHECK: bb.0:
304+
; CHECK-NEXT: liveins: $x10, $x11, $x13
305+
; CHECK-NEXT: {{ $}}
306+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
307+
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
308+
; CHECK-NEXT: PseudoBR %bb.3
309+
; CHECK-NEXT: {{ $}}
310+
; CHECK-NEXT: bb.1:
311+
; CHECK-NEXT: liveins: $x10, $x11, $x13
312+
; CHECK-NEXT: {{ $}}
313+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
314+
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
315+
; CHECK-NEXT: PseudoBR %bb.3
316+
; CHECK-NEXT: {{ $}}
317+
; CHECK-NEXT: bb.2:
318+
; CHECK-NEXT: liveins: $x10, $x11, $x13
319+
; CHECK-NEXT: {{ $}}
320+
; CHECK-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
321+
; CHECK-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
322+
; CHECK-NEXT: PseudoBR %bb.3
323+
; CHECK-NEXT: {{ $}}
324+
; CHECK-NEXT: bb.3:
325+
; CHECK-NEXT: PseudoRET
326+
;
327+
; CHECK-FS-LABEL: name: foo4
328+
; CHECK-FS: bb.0:
329+
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
330+
; CHECK-FS-NEXT: {{ $}}
331+
; CHECK-FS-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
332+
; CHECK-FS-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
333+
; CHECK-FS-NEXT: PseudoBR %bb.3
334+
; CHECK-FS-NEXT: {{ $}}
335+
; CHECK-FS-NEXT: bb.1:
336+
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
337+
; CHECK-FS-NEXT: {{ $}}
338+
; CHECK-FS-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
339+
; CHECK-FS-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
340+
; CHECK-FS-NEXT: PseudoBR %bb.3
341+
; CHECK-FS-NEXT: {{ $}}
342+
; CHECK-FS-NEXT: bb.2:
343+
; CHECK-FS-NEXT: liveins: $x10, $x11, $x13
344+
; CHECK-FS-NEXT: {{ $}}
345+
; CHECK-FS-NEXT: $x5 = PseudoCALLReg target-flags(riscv-call) @OUTLINED_FUNCTION_0, implicit-def $x5, implicit-def $x10, implicit-def $x11, implicit-def $x12, implicit $x10, implicit $x11
346+
; CHECK-FS-NEXT: $x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
347+
; CHECK-FS-NEXT: PseudoBR %bb.3
348+
; CHECK-FS-NEXT: {{ $}}
349+
; CHECK-FS-NEXT: bb.3:
350+
; CHECK-FS-NEXT: PseudoRET
351+
bb.0:
352+
liveins: $x10, $x11, $x13
353+
354+
$x11 = ORI $x11, 1023
355+
$x12 = ADDI $x10, 17
356+
$x11 = AND $x12, $x11
357+
$x10 = SUB $x10, $x11
358+
$x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
359+
PseudoBR %bb.3
360+
361+
bb.1:
362+
liveins: $x10, $x11, $x13
363+
364+
$x11 = ORI $x11, 1023
365+
$x12 = ADDI $x10, 17
366+
$x11 = AND $x12, $x11
367+
$x10 = SUB $x10, $x11
368+
$x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
369+
PseudoBR %bb.3
370+
371+
bb.2:
372+
liveins: $x10, $x11, $x13
373+
374+
$x11 = ORI $x11, 1023
375+
$x12 = ADDI $x10, 17
376+
$x11 = AND $x12, $x11
377+
$x10 = SUB $x10, $x11
378+
$x11 = LW killed renamable $x13, target-flags(riscv-pcrel-lo) <mcsymbol .Lpcrel_hi1> :: (dereferenceable load (s32) from @bar)
379+
PseudoBR %bb.3
380+
381+
bb.3:
382+
PseudoRET
383+
...

0 commit comments

Comments
 (0)