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 | |
| 22 | #include "Constants.h" |
| 23 | |
| 24 | namespace clspv { |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // The mangling loosely follows the Itanium convention. |
| 29 | // Its purpose is solely to ensure uniqueness of names, it is not |
| 30 | // meant to convey type information. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 31 | static std::string mangleType(Type *Ty) { |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 32 | if (Ty->isVectorTy()) { |
| 33 | auto NumVecElems = std::to_string(Ty->getVectorNumElements()); |
| 34 | return "Dv" + NumVecElems + "_" + mangleType(Ty->getScalarType()); |
| 35 | } else if (Ty->isIntegerTy()) { |
Kévin Petit | ff03aee | 2019-06-12 19:39:03 +0100 | [diff] [blame] | 36 | switch (Ty->getScalarSizeInBits()) { |
| 37 | case 1: |
| 38 | return "b"; |
| 39 | case 8: |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 40 | return "h"; |
Kévin Petit | ff03aee | 2019-06-12 19:39:03 +0100 | [diff] [blame] | 41 | case 16: |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 42 | return "t"; |
Kévin Petit | ff03aee | 2019-06-12 19:39:03 +0100 | [diff] [blame] | 43 | case 32: |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 44 | return "j"; |
Kévin Petit | ff03aee | 2019-06-12 19:39:03 +0100 | [diff] [blame] | 45 | case 64: |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 46 | return "m"; |
| 47 | } |
Kévin Petit | ff03aee | 2019-06-12 19:39:03 +0100 | [diff] [blame] | 48 | } else if (Ty->isFloatingPointTy()) { |
| 49 | switch (Ty->getScalarSizeInBits()) { |
| 50 | case 32: |
| 51 | return "f"; |
| 52 | case 64: |
| 53 | return "d"; |
| 54 | } |
Kévin Petit | 9b34026 | 2019-06-19 18:31:11 +0100 | [diff] [blame^] | 55 | } else if (Ty->isPointerTy()) { |
| 56 | auto PtrTy = cast<PointerType>(Ty); |
| 57 | auto AS = std::to_string(PtrTy->getAddressSpace()); |
| 58 | return "P" + AS + mangleType(PtrTy->getElementType()); |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | llvm_unreachable("Unhandled type in SPIR-V intrinsic name mangler"); |
| 62 | } |
| 63 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 64 | Instruction *InsertSPIRVOp(Instruction *Insert, spv::Op Opcode, |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 65 | ArrayRef<Attribute::AttrKind> Attributes, |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 66 | Type *RetType, ArrayRef<Value *> Args) { |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 67 | |
| 68 | // Prepare mangled name |
| 69 | std::string MangledName = clspv::SPIRVOpIntrinsicFunction(); |
| 70 | MangledName += std::to_string(Opcode); |
| 71 | MangledName += "."; |
| 72 | for (auto Arg : Args) { |
| 73 | MangledName += mangleType(Arg->getType()); |
| 74 | } |
| 75 | |
| 76 | // Create a function in the module |
| 77 | auto M = Insert->getModule(); |
| 78 | auto Int32Ty = Type::getInt32Ty(M->getContext()); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 79 | SmallVector<Type *, 8> ArgTypes = {Int32Ty}; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 80 | for (auto Arg : Args) { |
| 81 | ArgTypes.push_back(Arg->getType()); |
| 82 | } |
| 83 | auto NewFType = FunctionType::get(RetType, ArgTypes, false); |
| 84 | auto NewFTyC = M->getOrInsertFunction(MangledName, NewFType); |
| 85 | auto NewF = cast<Function>(NewFTyC.getCallee()); |
| 86 | for (auto A : Attributes) { |
| 87 | NewF->addFnAttr(A); |
| 88 | } |
| 89 | |
| 90 | // Now call it with the values we were passed |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 91 | SmallVector<Value *, 8> ArgValues = {ConstantInt::get(Int32Ty, Opcode)}; |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 92 | for (auto Arg : Args) { |
| 93 | ArgValues.push_back(Arg); |
| 94 | } |
| 95 | |
| 96 | return CallInst::Create(NewF, ArgValues, "", Insert); |
| 97 | } |
| 98 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 99 | }; // namespace clspv |