RISC-V port recognizes UNSPECs that may trap
Ensures RISC-V port correctly identifies UNSPECs that can cause exceptions, preventing scheduler mis-speculation.
This commit enhances the RISC-V backend’s ability to recognize ‘UNSPEC’ operations that can trigger floating-point exceptions, such as FE_INVALID. Previously, because these UNSPECs used integer modes, the compiler incorrectly assumed they could not trap, allowing the scheduler to move them too aggressively. The patch introduces a target-specific override (riscv_unspec_may_trap_p) to correctly flag these potentially trapping operations, preventing invalid speculation and ensuring correct program behavior.
In Details
The RISC-V port previously failed to correctly identify several UNSPECs with integer modes that could trigger floating-point exceptions (e.g., FE_INVALID). The TARGET_UNSPEC_MAY_TRAP_P hook returned false for these, leading the instruction scheduler to speculate trapping instructions across conditional branches. This change introduces riscv_unspec_may_trap_p as a target override for TARGET_UNSPEC_MAY_TRAP_P, ensuring these specific UNSPECs are correctly flagged as potentially trapping, thus preventing incorrect instruction reordering.
- RISC-V
- An open-standard instruction set architecture (ISA) based on Reduced Instruction Set Computer principles. GCC supports various RISC-V configurations.
- UNSPEC
- An internal GCC construct representing an operation that does not map directly to a standard RTL pattern but is specific to a particular backend or optimization. They are often used for internal compiler operations or target-specific intrinsics.
- FE_INVALID
- A floating-point exception flag indicating an invalid operation, such as division by zero or square root of a negative number.
- instruction scheduler
- A compiler pass responsible for reordering machine instructions to optimize for processor pipeline characteristics, such as instruction-level parallelism and minimizing stalls.
- speculation
- The process by which a compiler or processor predicts the outcome of a conditional branch or operation and begins executing instructions based on that prediction, potentially discarding the work if the prediction was wrong. This can apply to both instruction scheduling and hardware execution.