[SPIRV] Expand aggregate instructions with mutated-type uses (#206835)

This is a workaround for the problem in GH issue
https://github.com/llvm/llvm-project/issues/203586.

The problem is that we need to mutate aggregate-typed PHI nodes to
`i32`, however that means all incoming values need to have a matching
type, but we can't always easily mutate the incoming types to i32, as in
the case in the GH issue with a math intrinsic that returns a struct
by-value.

We do the same mutation for some other cases like `freeze`, the
workaround is the same.

The workaround is to expand the incoming values to a form that will also
be mutated to i32 so the types match. An example transformation is
below:

Before
```
...
bb1:                                              ; preds = %entry
  %0 = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %a, i64 %b)
  br label %epilog
...
  %2 = phi { i64, i1 } [ %1, %bb0 ], [ %0, %bb1 ]
```

After
```
...
bb1:                                              ; preds = %entry
  %0 = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %a, i64 %b)
  %1 = extractvalue { i64, i1 } %0, 0
  %2 = insertvalue { i64, i1 } poison, i64 %1, 0
  %3 = extractvalue { i64, i1 } %0, 1
  %4 = insertvalue { i64, i1 } %2, i1 %3, 1
  br label %epilog
...
  %10 = phi { i64, i1 } [ %9, %bb0 ], [ %4, %bb1 ]
```

`insertvalue` get replaced with a SPIR-V intrinsic later in the pass
which is also type-mutated, so the IR is correct:

```
...
bb1:                                              ; preds = %entry
  %0 = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %a, i64 %b)
  call void @llvm.spv.value.md(metadata !0)
  call void (...) @llvm.fake.use({ i64, i1 } %0)
  %1 = extractvalue { i64, i1 } %0, 0
  call void @llvm.spv.assign.type.i64(i64 %1, metadata i64 poison)
  call void @llvm.spv.assign.type.i32(i32 undef, metadata { i64, i1 } poison)
  %2 = call i32 (i32, i64, ...) @llvm.spv.insertv.i64(i32 undef, i64 %1, i32 0)
  call void @llvm.spv.assign.type.i32(i32 %2, metadata { i64, i1 } poison)
  %3 = extractvalue { i64, i1 } %0, 1
  call void @llvm.spv.assign.type.i1(i1 %3, metadata i1 poison)
  %4 = call i32 (i32, i1, ...) @llvm.spv.insertv.i1(i32 %2, i1 %3, i32 1)
  call void @llvm.spv.assign.type.i32(i32 %4, metadata { i64, i1 } poison)
  br label %epilog
...
  %10 = phi i32 [ %9, %bb0 ], [ %4, %bb1 ]
```

This is the simplest change I could come up with, my other ideas were
much more complicated and/or risky.

This fix is important for us because this pattern comes up in libc's
implementation on FMA, so this is blocking SPIR-V libc support.

Fixes: https://github.com/llvm/llvm-project/issues/203586

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
5 files changed
tree: f167841d02d4f36da332a0ffa6c9efec99e328ec
  1. .ci/
  2. .github/
  3. bolt/
  4. clang/
  5. clang-tools-extra/
  6. cmake/
  7. compiler-rt/
  8. cross-project-tests/
  9. flang/
  10. flang-rt/
  11. libc/
  12. libclc/
  13. libcxx/
  14. libcxxabi/
  15. libsycl/
  16. libunwind/
  17. lld/
  18. lldb/
  19. llvm/
  20. llvm-libgcc/
  21. mlir/
  22. offload/
  23. openmp/
  24. orc-rt/
  25. polly/
  26. runtimes/
  27. third-party/
  28. utils/
  29. .clang-format
  30. .clang-format-ignore
  31. .clang-tidy
  32. .git-blame-ignore-revs
  33. .gitattributes
  34. .gitignore
  35. .mailmap
  36. CODE_OF_CONDUCT.md
  37. CONTRIBUTING.md
  38. LICENSE.TXT
  39. pyproject.toml
  40. README.md
  41. SECURITY.md
README.md

The LLVM Compiler Infrastructure

OpenSSF Scorecard OpenSSF Best Practices libc++

Welcome to the LLVM project!

This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.

The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.

C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.

Other components include: the libc++ C++ standard library, the LLD linker, and more.

Getting the Source Code and Building LLVM

Consult the Getting Started with LLVM page for information on building and running LLVM.

For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.

Getting in touch

Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.

The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.