alan-baker | 869cd68 | 2021-03-05 11:21:19 -0500 | [diff] [blame] | 1 | // Copyright 2021 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/Pass.h" |
| 17 | |
| 18 | #include "Builtins.h" |
| 19 | #include "Passes.h" |
| 20 | #include "clspv/Option.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | class NativeMathPass : public ModulePass { |
| 26 | public: |
| 27 | static char ID; |
| 28 | NativeMathPass() : ModulePass(ID) {} |
| 29 | |
| 30 | bool runOnModule(Module &M) override; |
| 31 | }; |
| 32 | } // namespace |
| 33 | |
| 34 | char NativeMathPass::ID = 0; |
| 35 | INITIALIZE_PASS(NativeMathPass, "NativeMath", |
| 36 | "Replace some builtin library functions for faster, lower " |
| 37 | "precision alternatives", |
| 38 | false, false) |
| 39 | |
| 40 | namespace clspv { |
| 41 | ModulePass *createNativeMathPass() { return new NativeMathPass(); } |
| 42 | } // namespace clspv |
| 43 | |
| 44 | bool NativeMathPass::runOnModule(Module &M) { |
| 45 | if (!clspv::Option::NativeMath()) |
| 46 | return false; |
| 47 | |
| 48 | bool changed = false; |
| 49 | for (auto &F : M) { |
| 50 | auto info = clspv::Builtins::Lookup(F.getName()); |
| 51 | switch (info.getType()) { |
| 52 | case clspv::Builtins::kDistance: |
| 53 | case clspv::Builtins::kLength: |
| 54 | case clspv::Builtins::kFma: |
| 55 | case clspv::Builtins::kAcosh: |
| 56 | case clspv::Builtins::kAsinh: |
| 57 | case clspv::Builtins::kAtan: |
| 58 | case clspv::Builtins::kAtan2: |
| 59 | case clspv::Builtins::kAtanpi: |
| 60 | case clspv::Builtins::kAtan2pi: |
| 61 | case clspv::Builtins::kAtanh: |
| 62 | case clspv::Builtins::kFmod: |
| 63 | case clspv::Builtins::kFract: |
| 64 | case clspv::Builtins::kLdexp: |
| 65 | case clspv::Builtins::kRsqrt: |
| 66 | case clspv::Builtins::kHalfSqrt: |
| 67 | case clspv::Builtins::kSqrt: |
| 68 | case clspv::Builtins::kTanh: |
| 69 | // Strip the definition of the function leaving only the declaration. |
| 70 | changed = true; |
| 71 | F.deleteBody(); |
| 72 | break; |
| 73 | default: |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return changed; |
| 79 | } |