Skip to content

clang-20 crashed with optnone attribute at -O1 and above. error in backend: Cannot select: intrinsic %llvm.expect.with.probability. #115411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iamanonymouscs opened this issue Nov 8, 2024 · 4 comments · Fixed by #117459 or #117835
Assignees
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:SelectionDAG SelectionDAGISel as well

Comments

@iamanonymouscs
Copy link

clang-20 crashed with optnone attribute at -O1 and above.

Compiler explorer: https://godbolt.org/z/4dP5xGxnx

$cat mutant.c
int a;
void __attribute__((optnone)) b() {
  int c = __builtin_expect_with_probability(a, 0, 0.8);
}

Also crashed on clang-19.

$clang-19 -O1 mutant.c
fatal error: error in backend: Cannot select: intrinsic %llvm.expect.with.probability
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 19.0.0 (++20240722031324+65825cd5431c-1~exp1~20240722151445.1819)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/mutant-b18d19.c
clang: note: diagnostic msg: /tmp/mutant-b18d19.sh
clang: note: diagnostic msg: 

********************
@EugeneZelenko EugeneZelenko added backend:X86 crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:SelectionDAG SelectionDAGISel as well and removed new issue labels Nov 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/issue-subscribers-backend-x86

Author: Anonymous (iamanonymouscs)

clang-20 crashed with ```optnone``` attribute at ```-O1``` and above.

Compiler explorer: https://godbolt.org/z/4dP5xGxnx

$cat mutant.c
int a;
void __attribute__((optnone)) b() {
  int c = __builtin_expect_with_probability(a, 0, 0.8);
}

Also crashed on clang-19.

$clang-19 -O1 mutant.c
fatal error: error in backend: Cannot select: intrinsic %llvm.expect.with.probability
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 19.0.0 (++20240722031324+65825cd5431c-1~exp1~20240722151445.1819)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/mutant-b18d19.c
clang: note: diagnostic msg: /tmp/mutant-b18d19.sh
clang: note: diagnostic msg: 

********************

@phoebewang phoebewang added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed backend:X86 labels Nov 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2024

@llvm/issue-subscribers-clang-codegen

Author: Anonymous (iamanonymouscs)

clang-20 crashed with ```optnone``` attribute at ```-O1``` and above.

Compiler explorer: https://godbolt.org/z/4dP5xGxnx

$cat mutant.c
int a;
void __attribute__((optnone)) b() {
  int c = __builtin_expect_with_probability(a, 0, 0.8);
}

Also crashed on clang-19.

$clang-19 -O1 mutant.c
fatal error: error in backend: Cannot select: intrinsic %llvm.expect.with.probability
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 19.0.0 (++20240722031324+65825cd5431c-1~exp1~20240722151445.1819)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/mutant-b18d19.c
clang: note: diagnostic msg: /tmp/mutant-b18d19.sh
clang: note: diagnostic msg: 

********************

@phoebewang
Copy link
Contributor

I think this is Clang codegen problem. According to https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGBuiltin.cpp#L3522, we should not generate llvm.expect.with.probability for optnone as well.

@antangelo
Copy link
Contributor

That link seems to have drifted, but I presume it means to link to this:

// Don't generate llvm.expect.with.probability on -O0 as the backend
// won't use it for anything.
// Note, we still IRGen ExpectedValue because it could have side-effects.
if (CGM.getCodeGenOpts().OptimizationLevel == 0)
return RValue::get(ArgValue);

Curiously, if you replace __builtin_expect_with_probability with __builtin_expect, there is no assertion failure in the backend (despite the optimization check on the Clang side being exactly the same). See https://godbolt.org/z/abeh8Wba5

It seems like SelectionDAGBuilder, FastISel, and IntrinsicLowering each have similar handling that removes the llvm.expect if it has not already been transformed out, but no such handling exists for llvm.expect.with.probability. I think it should be handled here for consistency, but it probably doesn't hurt to check for optnone on the clang side too.

@antangelo antangelo self-assigned this Nov 17, 2024
antangelo added a commit that referenced this issue Nov 30, 2024
…when optimizations are disabled (#117835)

Handle @llvm.expect.with.probability in GlobalISel in the same way
@llvm.expect is handled, passing the value through as-is. This can be
encountered if the intrinsic is used without optimizations, which would
otherwise transform it out.

Fixes #115411 for GlobalISel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. crash Prefer [crash-on-valid] or [crash-on-invalid] llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
5 participants