| 1 |
//===- SPIRVMDBuilder.h - SPIR-V metadata builder header file --*- C++ -*-===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
/// \file |
| 35 |
/// |
| 36 |
/// This file declares classes for creating SPIR-V metadata. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_SPIRVMDBUILDER_H |
| 41 |
#define SPIRV_SPIRVMDBUILDER_H |
| 42 |
|
| 43 |
#include "SPIRVInternal.h" |
| 44 |
#include "llvm/IR/Metadata.h" |
| 45 |
|
| 46 |
#include <functional> |
| 47 |
using namespace llvm; |
| 48 |
|
| 49 |
namespace SPIRV { |
| 50 |
|
| 51 |
class SPIRVMDBuilder { |
| 52 |
public: |
| 53 |
template <typename ParentT> struct MDWrapper; |
| 54 |
struct NamedMDWrapper { |
| 55 |
NamedMDWrapper(NamedMDNode &Named, SPIRVMDBuilder &BB) |
| 56 |
: NMD(Named), B(BB) {} |
| 57 |
MDWrapper<NamedMDWrapper> addOp() { |
| 58 |
return MDWrapper<NamedMDWrapper>(*this, B); |
| 59 |
} |
| 60 |
NamedMDWrapper &addOp(MDWrapper<NamedMDWrapper> &MD) { |
| 61 |
NMD.addOperand(MD.M); |
| 62 |
return *this; |
| 63 |
} |
| 64 |
NamedMDNode &NMD; |
| 65 |
SPIRVMDBuilder &B; |
| 66 |
}; |
| 67 |
template <typename ParentT> struct MDWrapper { |
| 68 |
MDWrapper(ParentT &Parent, SPIRVMDBuilder &Builder) |
| 69 |
: M(nullptr), P(Parent), B(Builder) {} |
| 70 |
MDWrapper &add(unsigned I) { |
| 71 |
V.push_back(ConstantAsMetadata::get(getUInt32(&B.M, I))); |
| 72 |
return *this; |
| 73 |
} |
| 74 |
MDWrapper &addU16(unsigned short I) { |
| 75 |
V.push_back(ConstantAsMetadata::get(getUInt16(&B.M, I))); |
| 76 |
return *this; |
| 77 |
} |
| 78 |
MDWrapper &add(StringRef S) { |
| 79 |
V.push_back(MDString::get(B.C, S)); |
| 80 |
return *this; |
| 81 |
} |
| 82 |
MDWrapper &add(Function *F) { |
| 83 |
V.push_back(ConstantAsMetadata::get(F)); |
| 84 |
return *this; |
| 85 |
} |
| 86 |
MDWrapper &add(SmallVectorImpl<StringRef> &S) { |
| 87 |
for (auto &I : S) |
| 88 |
add(I); |
| 89 |
return *this; |
| 90 |
} |
| 91 |
MDWrapper &addOp(MDNode *Node) { |
| 92 |
V.push_back(Node); |
| 93 |
return *this; |
| 94 |
} |
| 95 |
MDWrapper<MDWrapper> addOp() { return MDWrapper<MDWrapper>(*this, B); } |
| 96 |
MDWrapper &addOp(MDWrapper<MDWrapper> &MD) { |
| 97 |
V.push_back(MD.M); |
| 98 |
return *this; |
| 99 |
} |
| 100 |
/// Generate the scheduled MDNode and return the parent. |
| 101 |
/// If \param Ptr is not nullptr, save the generated MDNode. |
| 102 |
ParentT &done(MDNode **Ptr = nullptr) { |
| 103 |
M = MDNode::get(B.C, V); |
| 104 |
if (Ptr) |
| 105 |
*Ptr = M; |
| 106 |
return P.addOp(*this); |
| 107 |
} |
| 108 |
MDNode *M; |
| 109 |
ParentT &P; |
| 110 |
SPIRVMDBuilder &B; |
| 111 |
SmallVector<Metadata *, 10> V; |
| 112 |
}; |
| 113 |
explicit SPIRVMDBuilder(Module &Mod) : M(Mod), C(Mod.getContext()) {} |
| 114 |
NamedMDWrapper addNamedMD(StringRef Name) { |
| 115 |
return NamedMDWrapper(*M.getOrInsertNamedMetadata(Name), *this); |
| 116 |
} |
| 117 |
SPIRVMDBuilder &eraseNamedMD(StringRef Name) { |
| 118 |
if (auto N = M.getNamedMetadata(Name)) |
| 119 |
M.eraseNamedMetadata(N); |
| 120 |
return *this; |
| 121 |
} |
| 122 |
friend struct NamedMDWrapper; |
| 123 |
|
| 124 |
private: |
| 125 |
Module &M; |
| 126 |
LLVMContext &C; |
| 127 |
}; |
| 128 |
|
| 129 |
} /* namespace SPIRV */ |
| 130 |
|
| 131 |
#endif // SPIRV_SPIRVMDBUILDER_H |