[llvm] r314568 - [AMDGPU] Set fast-math flags on functions given the options
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 2 10:54:26 PDT 2017
> On Sep 29, 2017, at 16:40, Stanislav Mekhanoshin via llvm-commits <llvm-commits at lists.llvm.org> wrote:
>
> Author: rampitec
> Date: Fri Sep 29 16:40:19 2017
> New Revision: 314568
>
> URL: http://llvm.org/viewvc/llvm-project?rev=314568&view=rev
> Log:
> [AMDGPU] Set fast-math flags on functions given the options
>
> We have a single library build without relaxation options.
> When inlined library functions remove fast math attributes
> from the functions they are integrated into.
>
> This patch sets relaxation attributes on the functions after
> linking provided corresponding relaxation options are given.
> Math instructions inside the inlined functions remain to have
> no fast flags, but inlining does not prevent fast math
> transformations of a surrounding caller code anymore.
>
> Differential Revision: https://reviews.llvm.org/D38325
>
> Added:
> llvm/trunk/test/CodeGen/AMDGPU/inline-attr.ll
> Modified:
> llvm/trunk/lib/Target/AMDGPU/AMDGPU.h
> llvm/trunk/lib/Target/AMDGPU/AMDGPULibCalls.cpp
> llvm/trunk/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
>
> Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPU.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPU.h?rev=314568&r1=314567&r2=314568&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/AMDGPU/AMDGPU.h (original)
> +++ llvm/trunk/lib/Target/AMDGPU/AMDGPU.h Fri Sep 29 16:40:19 2017
> @@ -23,6 +23,7 @@ class ModulePass;
> class Pass;
> class Target;
> class TargetMachine;
> +class TargetOptions;
> class PassRegistry;
> class Module;
>
> @@ -52,7 +53,7 @@ FunctionPass *createSIDebuggerInsertNops
> FunctionPass *createSIInsertWaitsPass();
> FunctionPass *createSIInsertWaitcntsPass();
> FunctionPass *createSIFixWWMLivenessPass();
> -FunctionPass *createAMDGPUSimplifyLibCallsPass();
> +FunctionPass *createAMDGPUSimplifyLibCallsPass(const TargetOptions &);
> FunctionPass *createAMDGPUUseNativeCallsPass();
> FunctionPass *createAMDGPUCodeGenPreparePass();
> FunctionPass *createAMDGPUMachineCFGStructurizerPass();
>
> Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPULibCalls.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPULibCalls.cpp?rev=314568&r1=314567&r2=314568&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/AMDGPU/AMDGPULibCalls.cpp (original)
> +++ llvm/trunk/lib/Target/AMDGPU/AMDGPULibCalls.cpp Fri Sep 29 16:40:19 2017
> @@ -30,6 +30,7 @@
> #include "llvm/IR/ValueSymbolTable.h"
> #include "llvm/Support/Debug.h"
> #include "llvm/Support/raw_ostream.h"
> +#include "llvm/Target/TargetOptions.h"
> #include <vector>
> #include <cmath>
>
> @@ -168,10 +169,13 @@ namespace {
>
> AMDGPULibCalls Simplifier;
>
> + const TargetOptions Options;
> +
> public:
> static char ID; // Pass identification
>
> - AMDGPUSimplifyLibCalls() : FunctionPass(ID) {
> + AMDGPUSimplifyLibCalls(const TargetOptions &Opt = TargetOptions())
> + : FunctionPass(ID), Options(Opt) {
> initializeAMDGPUSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
> }
>
> @@ -1680,14 +1684,34 @@ bool AMDGPULibCalls::evaluateCall(CallIn
> }
>
> // Public interface to the Simplify LibCalls pass.
> -FunctionPass *llvm::createAMDGPUSimplifyLibCallsPass() {
> - return new AMDGPUSimplifyLibCalls();
> +FunctionPass *llvm::createAMDGPUSimplifyLibCallsPass(const TargetOptions &Opt) {
> + return new AMDGPUSimplifyLibCalls(Opt);
> }
>
> FunctionPass *llvm::createAMDGPUUseNativeCallsPass() {
> return new AMDGPUUseNativeCalls();
> }
>
> +static bool setFastFlags(Function &F, const TargetOptions &Options) {
> + AttrBuilder B;
> +
> + if (Options.UnsafeFPMath || Options.NoInfsFPMath)
> + B.addAttribute("no-infs-fp-math", "true");
> + if (Options.UnsafeFPMath || Options.NoNaNsFPMath)
> + B.addAttribute("no-nans-fp-math", "true");
> + if (Options.UnsafeFPMath) {
> + B.addAttribute("less-precise-fpmad", "true”);
less-precise-fpmad was removed a while ago (although maybe there is a use for it)
> + B.addAttribute("unsafe-fp-math", "true");
> + }
> +
> + if (!B.hasAttributes())
> + return false;
> +
> + F.addAttributes(AttributeList::FunctionIndex, B);
> +
> + return true;
> +}
> +
> bool AMDGPUSimplifyLibCalls::runOnFunction(Function &F) {
> if (skipFunction(F))
> return false;
> @@ -1699,6 +1723,9 @@ bool AMDGPUSimplifyLibCalls::runOnFuncti
> F.printAsOperand(dbgs(), false, F.getParent());
> dbgs() << '\n';);
>
> + if (!EnablePreLink)
> + Changed |= setFastFlags(F, Options);
> +
> for (auto &BB : F) {
> for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
> // Ignore non-calls.
>
> Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp?rev=314568&r1=314567&r2=314568&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp (original)
> +++ llvm/trunk/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp Fri Sep 29 16:40:19 2017
> @@ -370,17 +370,18 @@ void AMDGPUTargetMachine::adjustPassMana
> PM.add(createAMDGPUAlwaysInlinePass(false));
> });
>
> + const auto &Opt = Options;
Since TargetOptions is an ugly, stateful hack that is mutated on each function based on the attributes I don’t think I trust this. This is essentially setting the attributes based on the presence of the very attributes you are setting.
I also think there needs to be a way to restrict this to specifically handle the known builtin functions. I think doing this correctly requires a Module or SCC pass that knows about the special builtins which don’t have the specified attributes.
-Matt
More information about the llvm-commits
mailing list