llvm-project/clang/test/CodeGen/fp-reassoc-pragma.cpp
Nikita Popov a105877646
[InstCombine] Remove some of the complexity-based canonicalization (#91185)
The idea behind this canonicalization is that it allows us to handle less
patterns, because we know that some will be canonicalized away. This is
indeed very useful to e.g. know that constants are always on the right.

However, this is only useful if the canonicalization is actually
reliable. This is the case for constants, but not for arguments: Moving
these to the right makes it look like the "more complex" expression is
guaranteed to be on the left, but this is not actually the case in
practice. It fails as soon as you replace the argument with another
instruction.

The end result is that it looks like things correctly work in tests,
while they actually don't. We use the "thwart complexity-based
canonicalization" trick to handle this in tests, but it's often a
challenge for new contributors to get this right, and based on the
regressions this PR originally exposed, we clearly don't get this right
in many cases.

For this reason, I think that it's better to remove this complexity
canonicalization. It will make it much easier to write tests for
commuted cases and make sure that they are handled.
2024-08-21 12:02:54 +02:00

93 lines
2.9 KiB
C++

// RUN: %clang_cc1 -O3 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s
// Simple case
float fp_reassoc_simple(float a, float b, float c) {
// CHECK: _Z17fp_reassoc_simplefff
// CHECK: %[[A:.+]] = fadd reassoc float %b, %c
// CHECK: %[[M:.+]] = fmul reassoc float %b, %[[A]]
// CHECK-NEXT: fadd reassoc float %c, %[[M]]
#pragma clang fp reassociate(on)
a = b + c;
return a * b + c;
}
// Reassoc pragma should only apply to its scope
float fp_reassoc_scoped(float a, float b, float c) {
// CHECK: _Z17fp_reassoc_scopedfff
// CHECK: %[[M:.+]] = fmul float %a, %b
// CHECK-NEXT: fadd float %[[M]], %c
{
#pragma clang fp reassociate(on)
}
return a * b + c;
}
// Reassoc pragma should apply to templates as well
class Foo {};
Foo operator+(Foo, Foo);
template <typename T>
T template_reassoc(T a, T b, T c) {
#pragma clang fp reassociate(on)
return ((a + b) - c) + c;
}
float fp_reassoc_template(float a, float b, float c) {
// CHECK: _Z19fp_reassoc_templatefff
// CHECK: %[[A1:.+]] = fadd reassoc float %a, %b
// CHECK-NEXT: %[[A2:.+]] = fsub reassoc float %[[A1]], %c
// CHECK-NEXT: fadd reassoc float %c, %[[A2]]
return template_reassoc<float>(a, b, c);
}
// File Scoping should work across functions
#pragma clang fp reassociate(on)
float fp_file_scope_on(float a, float b, float c) {
// CHECK: _Z16fp_file_scope_onfff
// CHECK: %[[M1:.+]] = fmul reassoc float %a, %c
// CHECK-NEXT: %[[M2:.+]] = fmul reassoc float %b, %c
// CHECK-NEXT: fadd reassoc float %[[M1]], %[[M2]]
return (a * c) + (b * c);
}
// Inner pragma has precedence
float fp_file_scope_stop(float a, float b, float c) {
// CHECK: _Z18fp_file_scope_stopfff
// CHECK: %[[A:.+]] = fadd reassoc float %a, %a
// CHECK: %[[M1:.+]] = fmul float %[[A]], %c
// CHECK-NEXT: %[[M2:.+]] = fmul float %b, %c
// CHECK-NEXT: fsub float %[[M1]], %[[M2]]
a = a + a;
{
#pragma clang fp reassociate(off)
return (a * c) - (b * c);
}
}
#pragma clang fp reassociate(off)
float fp_reassoc_off(float a, float b, float c) {
// CHECK: _Z14fp_reassoc_offfff
// CHECK: %[[D1:.+]] = fdiv float %a, %c
// CHECK-NEXT: %[[D2:.+]] = fdiv float %b, %c
// CHECK-NEXT: fadd float %[[D1]], %[[D2]]
return (a / c) + (b / c);
}
// Takes latest flag
float fp_reassoc_many(float a, float b, float c) {
// CHECK: _Z15fp_reassoc_manyfff
// CHECK: %[[D1:.+]] = fdiv reassoc float %a, %c
// CHECK-NEXT: %[[D2:.+]] = fdiv reassoc float %b, %c
// CHECK-NEXT: fadd reassoc float %[[D1]], %[[D2]]
#pragma clang fp reassociate(off) reassociate(on)
return (a / c) + (b / c);
}
// Pragma does not propagate through called functions
float helper_func(float a, float b, float c) { return a + b + c; }
float fp_reassoc_call_helper(float a, float b, float c) {
// CHECK: _Z22fp_reassoc_call_helperfff
// CHECK: %[[S1:.+]] = fadd float %a, %b
// CHECK-NEXT: fadd float %[[S1]], %c
#pragma clang fp reassociate(on)
return helper_func(a, b, c);
}