[SPIRV] Process indirect function calls immediately (#177222) The SPIR-V backend processes indirect function calls in `SPIRVCallLowering`, which is a subclass of the generic `CallLowering`. It intends to process them function by function, by first collecting all indirect calls in a function, and then processing all of a function's indirect calls at once when the call lowering for the function is about to end. Today, it relies on the `lowerReturn` virtual function to be called by the generic call lowering infra to know the function processing is about to end, and at that time it processes all of the collected indirect calls and clears the vector of indirect calls. The problem is that not all functions have return instructions. Every basic block must have a terminator instruction, but it does [not](https://llvm.org/docs/LangRef.html#terminator-instructions) have to be a return. In the failing case here, function `a` has an infinite loop, with every iteration having an indirect call and a check to see if it should break out of the infinite loop. There is a return instruction initially, but it is optimized out in the middle end. There is also an unrelated function `b` with no indirect calls. During call lowering, information about the indirect call in `a` is captured in the vector in SPIRVCallLowering, but since there is no return instruction, it is never processed. Then, function `b` is processed. `b` happens to have a return instruction, so the indirect call from `a` is processed, but the processing assumes that the indirect call is inside the function being processed, and it writes to a `{function, register}->SPIRVType` map, but the function is `b` not `a`. This ends up causing the map to have an invalid SPIRVType for the register in function `b`, and `b` happens to have a register of the same type, causing an invalid result from the map lookup and thus invalid IR (`G_ADDRSPACE_CAST` from `ptr addrspace(4)` to `i32`). We can't move the indirect call processing into a seperate pass because we lose required information after call lowering ends. Here, we change the indirect call processing to be done immediately when it is seen. Comments say the reason the collect all then process style was done was to get better type information for the caller of the indirect function if an opaque pointer is an arg to the caller and the indirect function, but [an existing test case](https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_function_pointers/fp-simple-hierarchy.ll) seems to exercise this, and the test still passes after this change and actually has the exact same assembly. The test case added is a minimal example from `llvm-reduce`, so it's just an infinite loop with no exit condition. Signed-off-by: Nick Sarnie <nick.sarnie@intel.com>
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.
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.
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.