Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [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/Constants.h" |
| 16 | #include "llvm/IR/IRBuilder.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Transforms/Utils/Cloning.h" |
| 21 | |
| 22 | #include "clspv/AddressSpace.h" |
| 23 | #include "clspv/Option.h" |
| 24 | #include "clspv/PushConstant.h" |
| 25 | |
| 26 | #include "Constants.h" |
| 27 | #include "Passes.h" |
| 28 | #include "PushConstant.h" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "declarepushconstants" |
| 33 | |
| 34 | namespace { |
| 35 | struct DeclarePushConstantsPass : public ModulePass { |
| 36 | static char ID; |
| 37 | DeclarePushConstantsPass() : ModulePass(ID) {} |
| 38 | |
| 39 | bool runOnModule(Module &M) override; |
| 40 | }; |
| 41 | } // namespace |
| 42 | |
| 43 | char DeclarePushConstantsPass::ID = 0; |
| 44 | INITIALIZE_PASS(DeclarePushConstantsPass, "DeclarePushConstants", |
| 45 | "Declare push constants", false, false) |
| 46 | |
| 47 | namespace clspv { |
| 48 | ModulePass *createDeclarePushConstantsPass() { |
| 49 | return new DeclarePushConstantsPass(); |
| 50 | } |
| 51 | } // namespace clspv |
| 52 | |
| 53 | bool DeclarePushConstantsPass::runOnModule(Module &M) { |
| 54 | |
| 55 | bool changed = false; |
| 56 | |
| 57 | std::vector<clspv::PushConstant> PushConstants; |
| 58 | |
| 59 | auto &C = M.getContext(); |
| 60 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 61 | if (clspv::ShouldDeclareGlobalOffsetPushConstant(M)) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 62 | PushConstants.emplace_back(clspv::PushConstant::GlobalOffset); |
| 63 | } |
| 64 | |
alan-baker | 6a3930b | 2020-05-21 10:09:11 -0400 | [diff] [blame] | 65 | if (clspv::ShouldDeclareEnqueuedLocalSizePushConstant(M)) { |
Kévin Petit | 1af73be | 2020-03-11 17:53:44 +0000 | [diff] [blame] | 66 | PushConstants.push_back(clspv::PushConstant::EnqueuedLocalSize); |
| 67 | } |
| 68 | |
alan-baker | 6a3930b | 2020-05-21 10:09:11 -0400 | [diff] [blame] | 69 | if (clspv::ShouldDeclareGlobalSizePushConstant(M)) { |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 70 | PushConstants.push_back(clspv::PushConstant::GlobalSize); |
| 71 | } |
| 72 | |
alan-baker | 6a3930b | 2020-05-21 10:09:11 -0400 | [diff] [blame] | 73 | if (clspv::ShouldDeclareRegionOffsetPushConstant(M)) { |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 74 | PushConstants.push_back(clspv::PushConstant::RegionOffset); |
| 75 | } |
| 76 | |
alan-baker | 6a3930b | 2020-05-21 10:09:11 -0400 | [diff] [blame] | 77 | if (clspv::ShouldDeclareNumWorkgroupsPushConstant(M)) { |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 78 | PushConstants.push_back(clspv::PushConstant::NumWorkgroups); |
| 79 | } |
| 80 | |
alan-baker | 6a3930b | 2020-05-21 10:09:11 -0400 | [diff] [blame] | 81 | if (clspv::ShouldDeclareRegionGroupOffsetPushConstant(M)) { |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 82 | PushConstants.push_back(clspv::PushConstant::RegionGroupOffset); |
| 83 | } |
| 84 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 85 | if (PushConstants.size() > 0) { |
| 86 | changed = true; |
| 87 | |
| 88 | std::vector<Type *> Members; |
| 89 | |
| 90 | for (auto &pc : PushConstants) { |
| 91 | Members.push_back(GetPushConstantType(M, pc)); |
| 92 | } |
| 93 | |
| 94 | auto STy = StructType::create(C, Members); |
| 95 | |
| 96 | auto GV = |
| 97 | new GlobalVariable(M, STy, false, GlobalValue::ExternalLinkage, nullptr, |
| 98 | clspv::PushConstantsVariableName(), nullptr, |
| 99 | GlobalValue::ThreadLocalMode::NotThreadLocal, |
| 100 | clspv::AddressSpace::PushConstant); |
| 101 | |
| 102 | GV->setInitializer(Constant::getNullValue(STy)); |
| 103 | |
| 104 | std::vector<llvm::Metadata *> MDArgs; |
| 105 | for (auto &pc : PushConstants) { |
| 106 | auto Cst = |
| 107 | ConstantInt::get(IntegerType::get(C, 32), static_cast<int>(pc)); |
| 108 | MDArgs.push_back(llvm::ConstantAsMetadata::get(Cst)); |
| 109 | }; |
| 110 | |
| 111 | GV->setMetadata(clspv::PushConstantsMetadataName(), |
| 112 | llvm::MDNode::get(C, MDArgs)); |
| 113 | } |
| 114 | |
| 115 | return changed; |
| 116 | } |