Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/libSPIRV/SPIRVValue.cpp

raw
1 //===- SPIRVValue.cpp - Class to represent a SPIR-V Value -------*- 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 defines the values defined in SPIR-V spec with op codes.
37 ///
38 /// The name of the SPIR-V values follow the op code name in the spec.
39 /// This is for readability and ease of using macro to handle types.
40 //
41 //===----------------------------------------------------------------------===//
42
43 #include "SPIRVValue.h"
44 #include "SPIRVEnum.h"
45
46 #include "llvm/ADT/APInt.h"
47
48 namespace SPIRV {
49 void SPIRVValue::setAlignment(SPIRVWord A) {
50 if (A == 0) {
51 eraseDecorate(DecorationAlignment);
52 return;
53 }
54 SPIRVWord PrevAlignment;
55 if (hasAlignment(&PrevAlignment)) {
56 // Do nothing if the Id already has an Alignment decoration, provided
57 // it matches the new alignment.
58 assert(A == PrevAlignment &&
59 "New alignment does not match existing alignment");
60 return;
61 }
62 addDecorate(new SPIRVDecorate(DecorationAlignment, this, A));
63 SPIRVDBG(spvdbgs() << "Set alignment " << A << " for obj " << Id << "\n")
64 }
65
66 bool SPIRVValue::hasAlignment(SPIRVWord *Result) const {
67 return hasDecorate(DecorationAlignment, 0, Result);
68 }
69
70 bool SPIRVValue::isVolatile() const { return hasDecorate(DecorationVolatile); }
71
72 void SPIRVValue::setVolatile(bool IsVolatile) {
73 if (!IsVolatile) {
74 eraseDecorate(DecorationVolatile);
75 return;
76 }
77 addDecorate(new SPIRVDecorate(DecorationVolatile, this));
78 SPIRVDBG(spvdbgs() << "Set volatile "
79 << " for obj " << Id << "\n")
80 }
81
82 bool SPIRVValue::hasNoSignedWrap() const {
83 return hasDecorate(DecorationNoSignedWrap);
84 }
85
86 bool SPIRVValue::hasNoUnsignedWrap() const {
87 return hasDecorate(DecorationNoUnsignedWrap);
88 }
89
90 void SPIRVValue::setFPFastMathMode(SPIRVWord M) {
91 if (M == 0) {
92 eraseDecorate(DecorationFPFastMathMode);
93 return;
94 }
95 addDecorate(new SPIRVDecorate(DecorationFPFastMathMode, this, M));
96 SPIRVDBG(spvdbgs() << "Set fast math mode to " << M << " for obj " << Id
97 << "\n")
98 }
99
100 template <spv::Decoration NoIntegerWrapDecoration>
101 void SPIRVValue::setNoIntegerDecorationWrap(bool HasNoIntegerWrap) {
102 if (!HasNoIntegerWrap) {
103 eraseDecorate(NoIntegerWrapDecoration);
104 return;
105 }
106 // NoSignedWrap and NoUnsignedWrap decorations are available only if it is
107 // allowed to use SPIR-V 1.4 or if SPV_KHR_no_integer_wrap_decoration
108 // extension is enabled
109 #ifdef _SPIRVDBG
110 const std::string InstStr =
111 NoIntegerWrapDecoration == DecorationNoSignedWrap ? "nsw" : "nuw";
112 #endif // _SPIRVDBG
113 if (Module->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
114 Module->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
115 addDecorate(new SPIRVDecorate(NoIntegerWrapDecoration, this));
116 SPIRVDBG(spvdbgs() << "Set " << InstStr << " for obj " << Id << "\n")
117 } else if (Module->isAllowedToUseExtension(
118 ExtensionID::SPV_KHR_no_integer_wrap_decoration)) {
119 Module->addExtension(ExtensionID::SPV_KHR_no_integer_wrap_decoration);
120 addDecorate(new SPIRVDecorate(NoIntegerWrapDecoration, this));
121 SPIRVDBG(spvdbgs() << "Set " << InstStr << " for obj " << Id << "\n")
122 } else {
123 SPIRVDBG(spvdbgs() << "Skip setting " << InstStr << " for obj " << Id
124 << "\n")
125 }
126 }
127
128 template void
129 SPIRVValue::setNoIntegerDecorationWrap<DecorationNoSignedWrap>(bool);
130 template void
131 SPIRVValue::setNoIntegerDecorationWrap<DecorationNoUnsignedWrap>(bool);
132
133 template <spv::Op OC>
134 void SPIRVConstantBase<OC>::setWords(const uint64_t *TheValue) {
135 assert(TheValue && "Nullptr value");
136 recalculateWordCount();
137 validate();
138
139 Words.resize(NumWords);
140 for (size_t I = 0; I != NumWords / 2; ++I) {
141 Words[I * 2] = static_cast<SPIRVWord>(TheValue[I]) & SPIRVWORD_MAX;
142 Words[I * 2 + 1] =
143 static_cast<SPIRVWord>((TheValue[I] >> SpirvWordBitWidth)) &
144 SPIRVWORD_MAX;
145 }
146 if (NumWords % 2)
147 Words.back() =
148 static_cast<SPIRVWord>(TheValue[NumWords / 2]) & SPIRVWORD_MAX;
149 }
150
151 // Complete constructor for AP integer constant
152 template <spv::Op OC>
153 SPIRVConstantBase<OC>::SPIRVConstantBase(SPIRVModule *M, SPIRVType *TheType,
154 SPIRVId TheId,
155 const llvm::APInt &TheValue)
156 : SPIRVValue(M, 0, OC, TheType, TheId) {
157 setWords(TheValue.getRawData());
158 }
159
160 // To solve errors about undefined reference to template class methods
161 // definitions.
162 template class SPIRVConstantBase<OpConstant>;
163 template class SPIRVConstantBase<OpSpecConstant>;
164
165 } // namespace SPIRV