55 changes: 55 additions & 0 deletions clang/test/PCH/pragma-floatcontrol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Test this without pch.
// RUN: %clang_cc1 %s -include %s -verify -fsyntax-only -DSET
// RUN: %clang_cc1 %s -include %s -verify -fsyntax-only -DPUSH
// RUN: %clang_cc1 %s -include %s -verify -fsyntax-only -DPUSH_POP

// Test with pch.
// RUN: %clang_cc1 %s -DSET -emit-pch -o %t
// RUN: %clang_cc1 %s -DSET -include-pch %t -emit-llvm -o - | FileCheck --check-prefix=CHECK-EBSTRICT %s
// RUN: %clang_cc1 %s -DPUSH -emit-pch -o %t
// RUN: %clang_cc1 %s -DPUSH -verify -include-pch %t
// RUN: %clang_cc1 %s -DPUSH_POP -emit-pch -o %t
// RUN: %clang_cc1 %s -DPUSH_POP -verify -include-pch %t

#ifndef HEADER
#define HEADER

#ifdef SET
#pragma float_control(except, on)
#endif

#ifdef PUSH
#pragma float_control(precise, on)
#pragma float_control(push)
#pragma float_control(precise, off)
#endif

#ifdef PUSH_POP
#pragma float_control(precise, on, push)
#pragma float_control(push)
#pragma float_control(pop)
#endif
#else

#ifdef SET
float fun(float a, float b) {
// CHECK-LABEL: define float @fun{{.*}}
//CHECK-EBSTRICT: llvm.experimental.constrained.fmul{{.*}}tonearest{{.*}}strict
//CHECK-EBSTRICT: llvm.experimental.constrained.fadd{{.*}}tonearest{{.*}}strict
return a * b + 2;
}
#pragma float_control(pop) // expected-warning {{#pragma float_control(pop, ...) failed: stack empty}}
#pragma float_control(pop) // expected-warning {{#pragma float_control(pop, ...) failed: stack empty}}
#endif

#ifdef PUSH
#pragma float_control(pop)
#pragma float_control(pop) // expected-warning {{#pragma float_control(pop, ...) failed: stack empty}}
#endif

#ifdef PUSH_POP
#pragma float_control(pop)
#pragma float_control(pop) // expected-warning {{#pragma float_control(pop, ...) failed: stack empty}}
#endif

#endif //ifndef HEADER
63 changes: 63 additions & 0 deletions clang/test/Parser/fp-floatcontrol-syntax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// RUN: %clang_cc1 -fsyntax-only -verify -DCHECK_ERROR %s

float function_scope(float a) {
#pragma float_control(precise, on) junk // expected-warning {{extra tokens at end of '#pragma float_control' - ignored}}
return a;
}

#ifdef CHECK_ERROR
#pragma float_control(push)
#pragma float_control(pop)
#pragma float_control(precise, on, push)
void check_stack() {
#pragma float_control(push) // expected-error {{can only appear at file scope}}
#pragma float_control(pop) // expected-error {{can only appear at file scope}}
#pragma float_control(precise, on, push) // expected-error {{can only appear at file scope}}
#pragma float_control(except, on, push) // expected-error {{can only appear at file scope}}
#pragma float_control(except, on, push, junk) // expected-error {{float_control is malformed}}
return;
}
#endif

// RUN: %clang -c -fsyntax-only %s -DDEFAULT -Xclang -verify
// RUN: %clang -c -fsyntax-only %s -ffp-model=precise -DPRECISE -Xclang -verify
// RUN: %clang -c -fsyntax-only %s -ffp-model=strict -DSTRICT -Xclang -verify
// RUN: %clang -c -fsyntax-only %s -ffp-model=fast -DFAST -Xclang -verify
double a = 0.0;
double b = 1.0;

//FIXME At some point this warning will be removed, until then
// document the warning
#ifdef FAST
// expected-warning@+1{{pragma STDC FENV_ACCESS ON is not supported, ignoring pragma}}
#pragma STDC FENV_ACCESS ON // expected-error{{'#pragma STDC FENV_ACCESS ON' is illegal when precise is disabled}}
#else
#pragma STDC FENV_ACCESS ON // expected-warning{{pragma STDC FENV_ACCESS ON is not supported, ignoring pragma}}
#endif
#ifdef STRICT
#pragma float_control(precise, off) // expected-error {{'#pragma float_control(precise, off)' is illegal when except is enabled}}
#else
#pragma float_control(precise, off) // expected-error {{'#pragma float_control(precise, off)' is illegal when fenv_access is enabled}}
#endif
//RUN -ffp-model=strict
//error: '#pragma float_control(precise, off)' is illegal when except is enabled
//with default, fast or precise: no errors

#pragma float_control(precise, on)
#pragma float_control(except, on) // OK
#ifndef STRICT
#pragma float_control(except, on)
#pragma float_control(precise, off) // expected-error {{'#pragma float_control(precise, off)' is illegal when except is enabled}}
#endif
int main() {
#ifdef STRICT
#pragma float_control(precise, off) // expected-error {{'#pragma float_control(precise, off)' is illegal when except is enabled}}
#else
#pragma float_control(precise, off) // expected-error {{'#pragma float_control(precise, off)' is illegal when except is enabled}}
#endif
#pragma float_control(except, on)
// error: '#pragma float_control(except, on)' is illegal when precise is disabled
double x = b / a; // only used for fp flag setting
if (a == a) // only used for fp flag setting
return 0; //(int)x;
}
13 changes: 12 additions & 1 deletion llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ class IRBuilderBase {
/// Get the flags to be applied to created floating point ops
FastMathFlags getFastMathFlags() const { return FMF; }

FastMathFlags &getFastMathFlags() { return FMF; }

/// Clear the fast-math flags.
void clearFastMathFlags() { FMF.clear(); }

Expand Down Expand Up @@ -332,17 +334,26 @@ class IRBuilderBase {
IRBuilderBase &Builder;
FastMathFlags FMF;
MDNode *FPMathTag;
bool IsFPConstrained;
fp::ExceptionBehavior DefaultConstrainedExcept;
RoundingMode DefaultConstrainedRounding;

public:
FastMathFlagGuard(IRBuilderBase &B)
: Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
: Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
IsFPConstrained(B.IsFPConstrained),
DefaultConstrainedExcept(B.DefaultConstrainedExcept),
DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}

FastMathFlagGuard(const FastMathFlagGuard &) = delete;
FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;

~FastMathFlagGuard() {
Builder.FMF = FMF;
Builder.DefaultFPMathTag = FPMathTag;
Builder.IsFPConstrained = IsFPConstrained;
Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
}
};

Expand Down