Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1 | // Copyright 2019 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 "SPIRVOp.h" |
| 16 | |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 22 | #include "Builtins.h" |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 23 | #include "Constants.h" |
| 24 | |
| 25 | namespace clspv { |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 29 | Instruction *InsertSPIRVOp(Instruction *Insert, spv::Op Opcode, |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 30 | ArrayRef<Attribute::AttrKind> Attributes, |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 31 | Type *RetType, ArrayRef<Value *> Args) { |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 32 | |
| 33 | // Prepare mangled name |
| 34 | std::string MangledName = clspv::SPIRVOpIntrinsicFunction(); |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 35 | MangledName += "."; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 36 | MangledName += std::to_string(Opcode); |
| 37 | MangledName += "."; |
| 38 | for (auto Arg : Args) { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 39 | MangledName += Builtins::GetMangledTypeName(Arg->getType()); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 42 | auto M = Insert->getModule(); |
| 43 | auto Int32Ty = Type::getInt32Ty(M->getContext()); |
alan-baker | 5f2e88e | 2020-12-07 15:24:04 -0500 | [diff] [blame] | 44 | Function *func = M->getFunction(MangledName); |
| 45 | if (!func) { |
| 46 | // Create a function in the module |
| 47 | SmallVector<Type *, 8> ArgTypes = {Int32Ty}; |
| 48 | for (auto Arg : Args) { |
| 49 | ArgTypes.push_back(Arg->getType()); |
| 50 | } |
| 51 | auto NewFType = FunctionType::get(RetType, ArgTypes, false); |
| 52 | auto NewFTyC = M->getOrInsertFunction(MangledName, NewFType); |
| 53 | func = cast<Function>(NewFTyC.getCallee()); |
| 54 | for (auto A : Attributes) { |
| 55 | func->addFnAttr(A); |
| 56 | } |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Now call it with the values we were passed |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 60 | SmallVector<Value *, 8> ArgValues = {ConstantInt::get(Int32Ty, Opcode)}; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 61 | for (auto Arg : Args) { |
| 62 | ArgValues.push_back(Arg); |
| 63 | } |
| 64 | |
alan-baker | 5f2e88e | 2020-12-07 15:24:04 -0500 | [diff] [blame] | 65 | return CallInst::Create(func, ArgValues, "", Insert); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Marco Antognini | 7e33840 | 2021-03-15 12:48:37 +0000 | [diff] [blame] | 68 | } // namespace clspv |