Skip to content

Commit 5b62398

Browse files
maksfbAlexisPerry
authored andcommitted
[BOLT] Skip optimization of functions with alt instructions (llvm#95172)
Alternative instructions in the Linux kernel may modify control flow in a function. As such, it is unsafe to optimize functions with alternative instructions until we properly support CFG alternatives. Previously, we marked functions with alt instructions before the emission, but that could be too late if we remove or replace instructions with alternatives. We could have marked functions as non-simple immediately after reading .altinstructions, but it's nice to be able to view functions after CFG is built. Thus assign the non-simple status after building CFG.
1 parent f219b19 commit 5b62398

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

bolt/lib/Rewrite/LinuxKernelRewriter.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ class LinuxKernelRewriter final : public MetadataRewriter {
273273

274274
/// Handle alternative instruction info from .altinstructions.
275275
Error readAltInstructions();
276+
void processAltInstructionsPostCFG();
276277
Error tryReadAltInstructions(uint32_t AltInstFeatureSize,
277278
bool AltInstHasPadLen, bool ParseOnly);
278-
Error rewriteAltInstructions();
279279

280280
/// Read .pci_fixup
281281
Error readPCIFixupTable();
@@ -326,6 +326,8 @@ class LinuxKernelRewriter final : public MetadataRewriter {
326326
if (Error E = processORCPostCFG())
327327
return E;
328328

329+
processAltInstructionsPostCFG();
330+
329331
return Error::success();
330332
}
331333

@@ -335,9 +337,6 @@ class LinuxKernelRewriter final : public MetadataRewriter {
335337
if (Error E = rewriteExceptionTable())
336338
return E;
337339

338-
if (Error E = rewriteAltInstructions())
339-
return E;
340-
341340
if (Error E = rewriteParaInstructions())
342341
return E;
343342

@@ -1486,12 +1485,11 @@ Error LinuxKernelRewriter::tryReadAltInstructions(uint32_t AltInstFeatureSize,
14861485
return Error::success();
14871486
}
14881487

1489-
Error LinuxKernelRewriter::rewriteAltInstructions() {
1490-
// Disable output of functions with alt instructions before the rewrite
1491-
// support is complete.
1488+
void LinuxKernelRewriter::processAltInstructionsPostCFG() {
1489+
// Disable optimization and output of functions with alt instructions before
1490+
// the rewrite support is complete. Alt instructions can modify the control
1491+
// flow, hence we may end up deleting seemingly unreachable code.
14921492
skipFunctionsWithAnnotation("AltInst");
1493-
1494-
return Error::success();
14951493
}
14961494

14971495
/// When the Linux kernel needs to handle an error associated with a given PCI

bolt/test/X86/linux-alt-instruction.s

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %s -o %t.o
77
# RUN: %clang %cflags -nostdlib %t.o -o %t.exe \
88
# RUN: -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
9-
# RUN: llvm-bolt %t.exe --print-normalized --alt-inst-feature-size=2 -o %t.out \
9+
# RUN: llvm-bolt %t.exe --print-cfg --alt-inst-feature-size=2 -o %t.out \
1010
# RUN: | FileCheck %s
1111

1212
## Older kernels used to have padlen field in alt_instr. Check compatibility.
@@ -15,7 +15,7 @@
1515
# RUN: %s -o %t.padlen.o
1616
# RUN: %clang %cflags -nostdlib %t.padlen.o -o %t.padlen.exe \
1717
# RUN: -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
18-
# RUN: llvm-bolt %t.padlen.exe --print-normalized --alt-inst-has-padlen -o %t.padlen.out \
18+
# RUN: llvm-bolt %t.padlen.exe --print-cfg --alt-inst-has-padlen -o %t.padlen.out \
1919
# RUN: | FileCheck %s
2020

2121
## Check with a larger size of "feature" field in alt_instr.
@@ -24,7 +24,7 @@
2424
# RUN: --defsym FEATURE_SIZE_4=1 %s -o %t.fs4.o
2525
# RUN: %clang %cflags -nostdlib %t.fs4.o -o %t.fs4.exe \
2626
# RUN: -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr,--no-pie
27-
# RUN: llvm-bolt %t.fs4.exe --print-normalized --alt-inst-feature-size=4 -o %t.fs4.out \
27+
# RUN: llvm-bolt %t.fs4.exe --print-cfg --alt-inst-feature-size=4 -o %t.fs4.out \
2828
# RUN: | FileCheck %s
2929

3030
## Check that out-of-bounds read is handled properly.
@@ -33,9 +33,9 @@
3333

3434
## Check that BOLT automatically detects structure fields in .altinstructions.
3535

36-
# RUN: llvm-bolt %t.exe --print-normalized -o %t.out | FileCheck %s
37-
# RUN: llvm-bolt %t.exe --print-normalized -o %t.padlen.out | FileCheck %s
38-
# RUN: llvm-bolt %t.exe --print-normalized -o %t.fs4.out | FileCheck %s
36+
# RUN: llvm-bolt %t.exe --print-cfg -o %t.out | FileCheck %s
37+
# RUN: llvm-bolt %t.exe --print-cfg -o %t.padlen.out | FileCheck %s
38+
# RUN: llvm-bolt %t.exe --print-cfg -o %t.fs4.out | FileCheck %s
3939

4040
# CHECK: BOLT-INFO: Linux kernel binary detected
4141
# CHECK: BOLT-INFO: parsed 2 alternative instruction entries

0 commit comments

Comments
 (0)