alan-baker | 04f3a95 | 2020-03-24 10:39:53 -0400 | [diff] [blame] | 1 | // Copyright 2020 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/Attributes.h" |
| 16 | #include "llvm/IR/Module.h" |
| 17 | #include "llvm/Pass.h" |
| 18 | |
| 19 | #include "Constants.h" |
| 20 | #include "Passes.h" |
| 21 | |
| 22 | #define DEBUG_TYPE "addfunctionattributes" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | class AddFunctionAttributesPass : public ModulePass { |
| 28 | public: |
| 29 | static char ID; |
| 30 | AddFunctionAttributesPass() : ModulePass(ID) {} |
| 31 | |
| 32 | bool runOnModule(Module &M) override; |
| 33 | }; |
| 34 | } // namespace |
| 35 | |
| 36 | char AddFunctionAttributesPass::ID = 0; |
| 37 | INITIALIZE_PASS(AddFunctionAttributesPass, "AddFunctionAttributes", |
| 38 | "Add function attributes to builtin functions", false, false) |
| 39 | |
| 40 | namespace clspv { |
| 41 | ModulePass *createAddFunctionAttributesPass() { |
| 42 | return new AddFunctionAttributesPass(); |
| 43 | } |
| 44 | } // namespace clspv |
| 45 | |
| 46 | bool AddFunctionAttributesPass::runOnModule(Module &M) { |
| 47 | bool changed = false; |
| 48 | |
| 49 | // Add ReadNone and Speculatable to literal sampler functions to avoid loop |
| 50 | // optimizations producing phis with them. |
| 51 | if (auto F = M.getFunction(clspv::TranslateSamplerInitializerFunction())) { |
| 52 | F->addFnAttr(Attribute::AttrKind::ReadNone); |
| 53 | F->addFnAttr(Attribute::AttrKind::Speculatable); |
| 54 | changed = true; |
| 55 | } |
| 56 | |
| 57 | return changed; |
| 58 | } |