llvm-capstone/clang/test/AST/ast-print-fp-pragmas.c
Serge Pavlov f7819ce166 [FPEnv] Allow CompoundStmt to keep FP options
This is a recommit of b822efc740,
reverted in dc34d8df4c. The commit caused
fails because the test ast-print-fp-pragmas.c did not specify particular
target, and it failed on targets which do not support constrained
intrinsics. The original commit message is below.

AST does not have special nodes for pragmas. Instead a pragma modifies
some state variables of Sema, which in turn results in modified
attributes of AST nodes. This technique applies to floating point
operations as well. Every AST node that can depend on FP options keeps
current set of them.

This technique works well for options like exception behavior or fast
math options. They represent instructions to the compiler how to modify
code generation for the affected nodes. However treatment of FP control
modes has problems with this technique. Modifying FP control mode
(like rounding direction) usually requires operations on hardware, like
writing to control registers. It must be done prior to the first
operation that depends on the control mode. In particular, such
operations are required for implementation of `pragma STDC FENV_ROUND`,
compiler should set up necessary rounding direction at the beginning of
compound statement where the pragma occurs. As there is no representation
for pragmas in AST, the code generation becomes a complicated task in
this case.

To solve this issue FP options are kept inside CompoundStmt. Unlike to FP
options in expressions, these does not affect any operation on FP values,
but only inform the codegen about the FP options that act in the body of
the statement. As all pragmas that modify FP environment may occurs only
at the start of compound statement or at global level, such solution
works for all relevant pragmas. The options are kept as a difference
from the options in the enclosing compound statement or default options,
it helps codegen to set only changed control modes.

Differential Revision: https://reviews.llvm.org/D123952
2022-07-03 17:06:26 +07:00

70 lines
1.8 KiB
C

// RUN: %clang_cc1 -fsyntax-only -triple x86_64-pc-linux -ast-print %s -o - | FileCheck %s
float func_1(float x, float y) {
#pragma STDC FENV_ACCESS ON
if (x != 0) {
return y;
}
return x + y;
}
// CHECK-LABEL: float func_1(float x, float y) {
// CHECK-NEXT: #pragma STDC FENV_ACCESS ON
// CHECK-NEXT: if (x != 0) {
// CHECK-NEXT: return y;
// CHECK-NEXT: }
// CHECK-NEXT: return x + y;
// CHECK-NEXT: }
float func_2(float x, float y) {
#pragma STDC FENV_ACCESS ON
if (x != 0) {
#pragma STDC FENV_ACCESS OFF
return y;
}
return x + y;
}
// CHECK-LABEL: float func_2(float x, float y) {
// CHECK-NEXT: #pragma STDC FENV_ACCESS ON
// CHECK-NEXT: if (x != 0) {
// CHECK-NEXT: #pragma STDC FENV_ACCESS OFF
// CHECK-NEXT: return y;
// CHECK-NEXT: }
// CHECK-NEXT: return x + y;
// CHECK-NEXT: }
float func_3(float x, float y) {
#pragma STDC FENV_ROUND FE_DOWNWARD
return x + y;
}
// CHECK-LABEL: float func_3(float x, float y) {
// CHECK-NEXT: #pragma STDC FENV_ROUND FE_DOWNWARD
// CHECK-NEXT: return x + y;
// CHECK-NEXT: }
float func_4(float x, float y, float z) {
#pragma STDC FENV_ACCESS ON
#pragma clang fp exceptions(maytrap)
#pragma STDC FENV_ROUND FE_UPWARD
if (z != 0) {
#pragma STDC FENV_ACCESS OFF
#pragma STDC FENV_ROUND FE_TOWARDZERO
return z + x;
}
return x + y;
}
// CHECK-LABEL: float func_4(float x, float y, float z) {
// CHECK-NEXT: #pragma STDC FENV_ACCESS ON
// CHECK-NEXT: #pragma clang fp exceptions(maytrap)
// CHECK-NEXT: #pragma STDC FENV_ROUND FE_UPWARD
// CHECK-NEXT: if (z != 0) {
// CHECK-NEXT: #pragma STDC FENV_ACCESS OFF
// CHECK-NEXT: #pragma STDC FENV_ROUND FE_TOWARDZERO
// CHECK-NEXT: return z + x;
// CHECK-NEXT: }
// CHECK-NEXT: return x + y;
// CHECK-NEXT: }