Skip to content

Commit 6254c4f

Browse files
kiranchandramohanbryanpkc
authored andcommitted
Changes to flang's fp-contract behaviour
1) All fma and contraction behvaviour follows from the -ffp-contract= flag setting. 2) At optimization level 0, -ffp-contract=fast flag will not be honoured. 3) At all other levels, -ffp-contract=fast will be the default behaviour and the flag is honoured.
1 parent d17ef3e commit 6254c4f

File tree

2 files changed

+80
-22
lines changed

2 files changed

+80
-22
lines changed

clang/lib/Driver/ToolChains/ClassicFlang.cpp

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -464,28 +464,6 @@ void ClassicFlang::ConstructJob(Compilation &C, const JobAction &JA,
464464
}
465465
}
466466

467-
// Enable FMA
468-
for (Arg *A: Args.filtered(options::OPT_Mfma_on, options::OPT_fma)) {
469-
A->claim();
470-
LowerCmdArgs.push_back("-x");
471-
LowerCmdArgs.push_back("172");
472-
LowerCmdArgs.push_back("0x40000000");
473-
LowerCmdArgs.push_back("-x");
474-
LowerCmdArgs.push_back("179");
475-
LowerCmdArgs.push_back("1");
476-
}
477-
478-
// Disable FMA
479-
for (Arg *A: Args.filtered(options::OPT_Mfma_off, options::OPT_nofma)) {
480-
A->claim();
481-
LowerCmdArgs.push_back("-x");
482-
LowerCmdArgs.push_back("171");
483-
LowerCmdArgs.push_back("0x40000000");
484-
LowerCmdArgs.push_back("-x");
485-
LowerCmdArgs.push_back("178");
486-
LowerCmdArgs.push_back("1");
487-
}
488-
489467
// For -fPIC set -x 62 8 for second part of Fortran frontend
490468
for (Arg *A: Args.filtered(options::OPT_fPIC)) {
491469
A->claim();
@@ -550,6 +528,59 @@ void ClassicFlang::ConstructJob(Compilation &C, const JobAction &JA,
550528
A->claim();
551529
}
552530

531+
// fp-contract=fast is the default
532+
bool EnableFPContraction = true;
533+
if (Arg *A = Args.getLastArg(options::OPT_ffp_contract,
534+
options::OPT_Mfma_on,
535+
options::OPT_fma,
536+
options::OPT_Mfma_off,
537+
options::OPT_nofma)) {
538+
auto Opt = A->getOption();
539+
if (Opt.matches(options::OPT_ffp_contract)) {
540+
StringRef Val = A->getValue();
541+
if ((Val == "fast") || (Val == "on")) {
542+
EnableFPContraction = true;
543+
} else if (Val == "off") {
544+
EnableFPContraction = false;
545+
} else {
546+
D.Diag(diag::err_drv_unsupported_option_argument)
547+
<< A->getOption().getName() << Val;
548+
}
549+
} else if(Opt.matches(options::OPT_Mfma_on) ||
550+
Opt.matches(options::OPT_fma)) {
551+
EnableFPContraction = true;
552+
} else {
553+
EnableFPContraction = false;
554+
}
555+
}
556+
557+
if(OptLevel == 0)
558+
EnableFPContraction = false;
559+
560+
// Emit contract math instructions.
561+
// Step 1 : Generate fma instructions in flang (can override with fma flag)
562+
// Step 2 : Propagate fma contract information to LLVM to further
563+
// exploit contraction opportunities
564+
if (EnableFPContraction) {
565+
LowerCmdArgs.push_back("-x");
566+
LowerCmdArgs.push_back("172");
567+
LowerCmdArgs.push_back("0x40000000");
568+
LowerCmdArgs.push_back("-x");
569+
LowerCmdArgs.push_back("179");
570+
LowerCmdArgs.push_back("1");
571+
// Step 2
572+
LowerCmdArgs.push_back("-x");
573+
LowerCmdArgs.push_back("216");
574+
LowerCmdArgs.push_back("0x1000");
575+
} else {
576+
LowerCmdArgs.push_back("-x");
577+
LowerCmdArgs.push_back("171");
578+
LowerCmdArgs.push_back("0x40000000");
579+
LowerCmdArgs.push_back("-x");
580+
LowerCmdArgs.push_back("178");
581+
LowerCmdArgs.push_back("1");
582+
}
583+
553584
if (NeedFastMath) {
554585
// Lower: -x 216 1
555586
LowerCmdArgs.push_back("-x");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
! REQUIRES: classic_flang
2+
3+
! RUN: %flang -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
4+
! RUN: %flang -O1 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
5+
! RUN: %flang -O2 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
6+
! RUN: %flang -O3 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
7+
! RUN: %flang -Ofast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
8+
! RUN: %flang -ffp-contract=fast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
9+
! RUN: %flang -O1 -ffp-contract=fast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
10+
! RUN: %flang -O2 -ffp-contract=fast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
11+
! RUN: %flang -O3 -ffp-contract=fast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
12+
! RUN: %flang -Ofast -ffp-contract=fast -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
13+
! RUN: %flang -ffp-contract=on -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
14+
! RUN: %flang -O1 -ffp-contract=on -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
15+
! RUN: %flang -O2 -ffp-contract=on -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
16+
! RUN: %flang -O3 -ffp-contract=on -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
17+
! RUN: %flang -Ofast -ffp-contract=on -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT
18+
! RUN: %flang -ffp-contract=off -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
19+
! RUN: %flang -O1 -ffp-contract=off -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
20+
! RUN: %flang -O2 -ffp-contract=off -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
21+
! RUN: %flang -O3 -ffp-contract=off -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
22+
! RUN: %flang -Ofast -ffp-contract=off -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FLANG2-FP-CONTRACT-ABSENCE
23+
24+
! CHECK-FLANG2-FP-CONTRACT: "{{.*}}flang2"
25+
! CHECK-FLANG2-FP-CONTRACT-SAME: "-x" "172" "0x40000000" "-x" "179" "1" "-x" "216" "0x1000"
26+
! CHECK-FLANG2-FP-CONTRACT-ABSENCE: "{{.*}}flang2"
27+
! CHECK-FLANG2-FP-CONTRACT-ABSENCE-SAME: "-x" "171" "0x40000000" "-x" "178" "1"

0 commit comments

Comments
 (0)