| 1 |
//===- PreprocessMetadata.cpp - - 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 |
// |
| 35 |
// This file implements preprocessing of LLVM IR metadata in order to perform |
| 36 |
// further translation to SPIR-V. |
| 37 |
// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
#define DEBUG_TYPE "clmdtospv" |
| 40 |
|
| 41 |
#include "PreprocessMetadata.h" |
| 42 |
#include "OCLUtil.h" |
| 43 |
#include "SPIRVInternal.h" |
| 44 |
#include "SPIRVMDBuilder.h" |
| 45 |
#include "SPIRVMDWalker.h" |
| 46 |
#include "VectorComputeUtil.h" |
| 47 |
#include "libSPIRV/SPIRVDebug.h" |
| 48 |
|
| 49 |
#include "llvm/ADT/Triple.h" |
| 50 |
#include "llvm/IR/IRBuilder.h" |
| 51 |
#include "llvm/IR/InstVisitor.h" |
| 52 |
#include "llvm/Support/CommandLine.h" |
| 53 |
|
| 54 |
using namespace llvm; |
| 55 |
using namespace SPIRV; |
| 56 |
using namespace OCLUtil; |
| 57 |
|
| 58 |
namespace SPIRV { |
| 59 |
|
| 60 |
cl::opt<bool> EraseOCLMD("spirv-erase-cl-md", cl::init(true), |
| 61 |
cl::desc("Erase OpenCL metadata")); |
| 62 |
|
| 63 |
char PreprocessMetadataLegacy::ID = 0; |
| 64 |
|
| 65 |
bool PreprocessMetadataLegacy::runOnModule(Module &Module) { |
| 66 |
return runPreprocessMetadata(Module); |
| 67 |
} |
| 68 |
|
| 69 |
llvm::PreservedAnalyses |
| 70 |
PreprocessMetadataPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { |
| 71 |
return runPreprocessMetadata(M) ? llvm::PreservedAnalyses::none() |
| 72 |
: llvm::PreservedAnalyses::all(); |
| 73 |
} |
| 74 |
|
| 75 |
bool PreprocessMetadataBase::runPreprocessMetadata(Module &Module) { |
| 76 |
M = &Module; |
| 77 |
Ctx = &M->getContext(); |
| 78 |
|
| 79 |
LLVM_DEBUG(dbgs() << "Enter PreprocessMetadata:\n"); |
| 80 |
visit(M); |
| 81 |
LLVM_DEBUG(dbgs() << "After PreprocessMetadata:\n" << *M); |
| 82 |
|
| 83 |
verifyRegularizationPass(*M, "PreprocessMetadata"); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void PreprocessMetadataBase::preprocessCXXStructorList( |
| 89 |
SPIRVMDBuilder::NamedMDWrapper &EM, GlobalVariable *V, |
| 90 |
ExecutionMode EMode) { |
| 91 |
auto *List = dyn_cast_or_null<ConstantArray>(V->getInitializer()); |
| 92 |
if (!List) |
| 93 |
return; |
| 94 |
|
| 95 |
for (Value *V : List->operands()) { |
| 96 |
auto *Structor = cast<ConstantStruct>(V); |
| 97 |
|
| 98 |
// Each entry in the list is a struct containing 3 members: |
| 99 |
// (priority, function, data), with function being the entry point. |
| 100 |
auto *Kernel = cast<Function>(Structor->getOperand(1)); |
| 101 |
|
| 102 |
EM.addOp().add(Kernel).add(EMode).done(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void PreprocessMetadataBase::visit(Module *M) { |
| 107 |
SPIRVMDBuilder B(*M); |
| 108 |
SPIRVMDWalker W(*M); |
| 109 |
|
| 110 |
preprocessOCLMetadata(M, &B, &W); |
| 111 |
preprocessVectorComputeMetadata(M, &B, &W); |
| 112 |
|
| 113 |
// Create metadata representing (empty so far) list |
| 114 |
// of OpExecutionMode instructions |
| 115 |
auto EM = B.addNamedMD(kSPIRVMD::ExecutionMode); // !spirv.ExecutionMode = {} |
| 116 |
|
| 117 |
// Process special variables in LLVM IR module. |
| 118 |
if (auto *GV = M->getGlobalVariable("llvm.global_ctors")) |
| 119 |
preprocessCXXStructorList(EM, GV, spv::ExecutionModeInitializer); |
| 120 |
|
| 121 |
// Add execution modes for kernels. We take it from metadata attached to |
| 122 |
// the kernel functions. |
| 123 |
for (Function &Kernel : *M) { |
| 124 |
if (Kernel.getCallingConv() != CallingConv::SPIR_KERNEL) |
| 125 |
continue; |
| 126 |
|
| 127 |
// Specifing execution modes for the Kernel and adding it to the list |
| 128 |
// of ExecutionMode instructions. |
| 129 |
|
| 130 |
// !{void (i32 addrspace(1)*)* @kernel, i32 17, i32 X, i32 Y, i32 Z} |
| 131 |
if (MDNode *WGSize = Kernel.getMetadata(kSPIR2MD::WGSize)) { |
| 132 |
assert(WGSize->getNumOperands() >= 1 && WGSize->getNumOperands() <= 3 && |
| 133 |
"reqd_work_group_size does not have between 1 and 3 operands."); |
| 134 |
SmallVector<unsigned, 3> DecodedVals = decodeMDNode(WGSize); |
| 135 |
EM.addOp() |
| 136 |
.add(&Kernel) |
| 137 |
.add(spv::ExecutionModeLocalSize) |
| 138 |
.add(DecodedVals[0]) |
| 139 |
.add(DecodedVals.size() >= 2 ? DecodedVals[1] : 1) |
| 140 |
.add(DecodedVals.size() == 3 ? DecodedVals[2] : 1) |
| 141 |
.done(); |
| 142 |
} |
| 143 |
|
| 144 |
// !{void (i32 addrspace(1)*)* @kernel, i32 18, i32 X, i32 Y, i32 Z} |
| 145 |
if (MDNode *WGSizeHint = Kernel.getMetadata(kSPIR2MD::WGSizeHint)) { |
| 146 |
assert(WGSizeHint->getNumOperands() >= 1 && |
| 147 |
WGSizeHint->getNumOperands() <= 3 && |
| 148 |
"work_group_size_hint does not have between 1 and 3 operands."); |
| 149 |
SmallVector<unsigned, 3> DecodedVals = decodeMDNode(WGSizeHint); |
| 150 |
EM.addOp() |
| 151 |
.add(&Kernel) |
| 152 |
.add(spv::ExecutionModeLocalSizeHint) |
| 153 |
.add(DecodedVals[0]) |
| 154 |
.add(DecodedVals.size() >= 2 ? DecodedVals[1] : 1) |
| 155 |
.add(DecodedVals.size() == 3 ? DecodedVals[2] : 1) |
| 156 |
.done(); |
| 157 |
} |
| 158 |
|
| 159 |
// !{void (i32 addrspace(1)*)* @kernel, i32 30, i32 hint} |
| 160 |
if (MDNode *VecTypeHint = Kernel.getMetadata(kSPIR2MD::VecTyHint)) { |
| 161 |
EM.addOp() |
| 162 |
.add(&Kernel) |
| 163 |
.add(spv::ExecutionModeVecTypeHint) |
| 164 |
.add(transVecTypeHint(VecTypeHint)) |
| 165 |
.done(); |
| 166 |
} |
| 167 |
|
| 168 |
// !{void (i32 addrspace(1)*)* @kernel, i32 35, i32 size} |
| 169 |
if (MDNode *ReqdSubgroupSize = Kernel.getMetadata(kSPIR2MD::SubgroupSize)) { |
| 170 |
EM.addOp() |
| 171 |
.add(&Kernel) |
| 172 |
.add(spv::ExecutionModeSubgroupSize) |
| 173 |
.add(getMDOperandAsInt(ReqdSubgroupSize, 0)) |
| 174 |
.done(); |
| 175 |
} |
| 176 |
|
| 177 |
// !{void (i32 addrspace(1)*)* @kernel, i32 max_work_group_size, i32 X, |
| 178 |
// i32 Y, i32 Z} |
| 179 |
if (MDNode *MaxWorkgroupSizeINTEL = |
| 180 |
Kernel.getMetadata(kSPIR2MD::MaxWGSize)) { |
| 181 |
assert(MaxWorkgroupSizeINTEL->getNumOperands() == 3 && |
| 182 |
"max_work_group_size does not have 3 operands."); |
| 183 |
SmallVector<unsigned, 3> DecodedVals = |
| 184 |
decodeMDNode(MaxWorkgroupSizeINTEL); |
| 185 |
EM.addOp() |
| 186 |
.add(&Kernel) |
| 187 |
.add(spv::ExecutionModeMaxWorkgroupSizeINTEL) |
| 188 |
.add(DecodedVals[0]) |
| 189 |
.add(DecodedVals[1]) |
| 190 |
.add(DecodedVals[2]) |
| 191 |
.done(); |
| 192 |
} |
| 193 |
|
| 194 |
// !{void (i32 addrspace(1)*)* @kernel, i32 no_global_work_offset} |
| 195 |
if (Kernel.getMetadata(kSPIR2MD::NoGlobalOffset)) { |
| 196 |
EM.addOp().add(&Kernel).add(spv::ExecutionModeNoGlobalOffsetINTEL).done(); |
| 197 |
} |
| 198 |
|
| 199 |
// !{void (i32 addrspace(1)*)* @kernel, i32 max_global_work_dim, i32 dim} |
| 200 |
if (MDNode *MaxWorkDimINTEL = Kernel.getMetadata(kSPIR2MD::MaxWGDim)) { |
| 201 |
EM.addOp() |
| 202 |
.add(&Kernel) |
| 203 |
.add(spv::ExecutionModeMaxWorkDimINTEL) |
| 204 |
.add(getMDOperandAsInt(MaxWorkDimINTEL, 0)) |
| 205 |
.done(); |
| 206 |
} |
| 207 |
|
| 208 |
// !{void (i32 addrspace(1)*)* @kernel, i32 num_simd_work_items, i32 num} |
| 209 |
if (MDNode *NumSIMDWorkitemsINTEL = Kernel.getMetadata(kSPIR2MD::NumSIMD)) { |
| 210 |
EM.addOp() |
| 211 |
.add(&Kernel) |
| 212 |
.add(spv::ExecutionModeNumSIMDWorkitemsINTEL) |
| 213 |
.add(getMDOperandAsInt(NumSIMDWorkitemsINTEL, 0)) |
| 214 |
.done(); |
| 215 |
} |
| 216 |
|
| 217 |
// !{void (i32 addrspace(1)*)* @kernel, i32 scheduler_target_fmax_mhz, |
| 218 |
// i32 num} |
| 219 |
if (MDNode *SchedulerTargetFmaxMhzINTEL = |
| 220 |
Kernel.getMetadata(kSPIR2MD::FmaxMhz)) { |
| 221 |
EM.addOp() |
| 222 |
.add(&Kernel) |
| 223 |
.add(spv::ExecutionModeSchedulerTargetFmaxMhzINTEL) |
| 224 |
.add(getMDOperandAsInt(SchedulerTargetFmaxMhzINTEL, 0)) |
| 225 |
.done(); |
| 226 |
} |
| 227 |
|
| 228 |
// !{void (i32 addrspace(1)*)* @kernel, i32 ip_interface, i32 interface} |
| 229 |
if (MDNode *Interface = |
| 230 |
Kernel.getMetadata(kSPIR2MD::IntelFPGAIPInterface)) { |
| 231 |
std::set<std::string> InterfaceStrSet; |
| 232 |
// Default mode is 'csr' aka !ip_interface !N |
| 233 |
// !N = !{!”csr”} |
| 234 |
// don't emit any particular SPIR-V for it |
| 235 |
// Streaming mode metadata be like: |
| 236 |
// Not 'stall free' mode (to be mapped on '0' literal) |
| 237 |
// !ip_interface !N |
| 238 |
// !N = !{!"streaming"} |
| 239 |
// 'stall free' mode (to be mapped on '1' literal) |
| 240 |
// !ip_interface !N |
| 241 |
// !N = !{!"streaming", !"stall_free_return"} |
| 242 |
for (size_t I = 0; I != Interface->getNumOperands(); ++I) |
| 243 |
InterfaceStrSet.insert(getMDOperandAsString(Interface, I).str()); |
| 244 |
if (InterfaceStrSet.find("streaming") != InterfaceStrSet.end()) { |
| 245 |
int32_t InterfaceMode = 0; |
| 246 |
if (InterfaceStrSet.find("stall_free_return") != InterfaceStrSet.end()) |
| 247 |
InterfaceMode = 1; |
| 248 |
EM.addOp() |
| 249 |
.add(&Kernel) |
| 250 |
.add(spv::ExecutionModeStreamingInterfaceINTEL) |
| 251 |
.add(InterfaceMode) |
| 252 |
.done(); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void PreprocessMetadataBase::preprocessOCLMetadata(Module *M, SPIRVMDBuilder *B, |
| 259 |
SPIRVMDWalker *W) { |
| 260 |
unsigned CLVer = getOCLVersion(M, true); |
| 261 |
if (CLVer == 0) |
| 262 |
return; |
| 263 |
// Preprocess OpenCL-specific metadata |
| 264 |
// !spirv.Source = !{!x} |
| 265 |
// !{x} = !{i32 3, i32 102000} |
| 266 |
B->addNamedMD(kSPIRVMD::Source) |
| 267 |
.addOp() |
| 268 |
.add(CLVer == kOCLVer::CL21 ? spv::SourceLanguageOpenCL_CPP |
| 269 |
: spv::SourceLanguageOpenCL_C) |
| 270 |
.add(CLVer) |
| 271 |
.done(); |
| 272 |
if (EraseOCLMD) |
| 273 |
B->eraseNamedMD(kSPIR2MD::OCLVer).eraseNamedMD(kSPIR2MD::SPIRVer); |
| 274 |
|
| 275 |
// !spirv.MemoryModel = !{!x} |
| 276 |
// !{x} = !{i32 1, i32 2} |
| 277 |
Triple TT(M->getTargetTriple()); |
| 278 |
assert(isSupportedTriple(TT) && "Invalid triple"); |
| 279 |
B->addNamedMD(kSPIRVMD::MemoryModel) |
| 280 |
.addOp() |
| 281 |
.add(TT.isArch32Bit() ? spv::AddressingModelPhysical32 |
| 282 |
: spv::AddressingModelPhysical64) |
| 283 |
.add(spv::MemoryModelOpenCL) |
| 284 |
.done(); |
| 285 |
|
| 286 |
// Add source extensions |
| 287 |
// !spirv.SourceExtension = !{!x, !y, ...} |
| 288 |
// !x = {!"cl_khr_..."} |
| 289 |
// !y = {!"cl_khr_..."} |
| 290 |
auto Exts = getNamedMDAsStringSet(M, kSPIR2MD::Extensions); |
| 291 |
if (!Exts.empty()) { |
| 292 |
auto N = B->addNamedMD(kSPIRVMD::SourceExtension); |
| 293 |
for (auto &I : Exts) |
| 294 |
N.addOp().add(I).done(); |
| 295 |
} |
| 296 |
if (EraseOCLMD) |
| 297 |
B->eraseNamedMD(kSPIR2MD::Extensions).eraseNamedMD(kSPIR2MD::OptFeatures); |
| 298 |
|
| 299 |
if (EraseOCLMD) |
| 300 |
B->eraseNamedMD(kSPIR2MD::FPContract); |
| 301 |
} |
| 302 |
|
| 303 |
void PreprocessMetadataBase::preprocessVectorComputeMetadata(Module *M, |
| 304 |
SPIRVMDBuilder *B, |
| 305 |
SPIRVMDWalker *W) { |
| 306 |
using namespace VectorComputeUtil; |
| 307 |
|
| 308 |
auto EM = B->addNamedMD(kSPIRVMD::ExecutionMode); |
| 309 |
|
| 310 |
for (auto &F : *M) { |
| 311 |
if (F.getCallingConv() != CallingConv::SPIR_KERNEL) |
| 312 |
continue; |
| 313 |
|
| 314 |
// Add VC float control execution modes |
| 315 |
// RoundMode and FloatMode are always same for all types in VC |
| 316 |
// While Denorm could be different for double, float and half |
| 317 |
auto Attrs = F.getAttributes(); |
| 318 |
if (Attrs.hasFnAttr(kVCMetadata::VCFloatControl)) { |
| 319 |
SPIRVWord Mode = 0; |
| 320 |
Attrs.getFnAttr(kVCMetadata::VCFloatControl) |
| 321 |
.getValueAsString() |
| 322 |
.getAsInteger(0, Mode); |
| 323 |
spv::ExecutionMode ExecRoundMode = |
| 324 |
FPRoundingModeExecModeMap::map(getFPRoundingMode(Mode)); |
| 325 |
spv::ExecutionMode ExecFloatMode = |
| 326 |
FPOperationModeExecModeMap::map(getFPOperationMode(Mode)); |
| 327 |
VCFloatTypeSizeMap::foreach ( |
| 328 |
[&](VCFloatType FloatType, unsigned TargetWidth) { |
| 329 |
EM.addOp().add(&F).add(ExecRoundMode).add(TargetWidth).done(); |
| 330 |
EM.addOp().add(&F).add(ExecFloatMode).add(TargetWidth).done(); |
| 331 |
EM.addOp() |
| 332 |
.add(&F) |
| 333 |
.add(FPDenormModeExecModeMap::map( |
| 334 |
getFPDenormMode(Mode, FloatType))) |
| 335 |
.add(TargetWidth) |
| 336 |
.done(); |
| 337 |
}); |
| 338 |
} |
| 339 |
if (Attrs.hasFnAttr(kVCMetadata::VCSLMSize)) { |
| 340 |
SPIRVWord SLMSize = 0; |
| 341 |
Attrs.getFnAttr(kVCMetadata::VCSLMSize) |
| 342 |
.getValueAsString() |
| 343 |
.getAsInteger(0, SLMSize); |
| 344 |
EM.addOp() |
| 345 |
.add(&F) |
| 346 |
.add(spv::ExecutionModeSharedLocalMemorySizeINTEL) |
| 347 |
.add(SLMSize) |
| 348 |
.done(); |
| 349 |
} |
| 350 |
|
| 351 |
if (Attrs.hasFnAttr(kVCMetadata::VCNamedBarrierCount)) { |
| 352 |
SPIRVWord NBarrierCnt = 0; |
| 353 |
Attrs.getFnAttr(kVCMetadata::VCNamedBarrierCount) |
| 354 |
.getValueAsString() |
| 355 |
.getAsInteger(0, NBarrierCnt); |
| 356 |
EM.addOp() |
| 357 |
.add(&F) |
| 358 |
.add(spv::ExecutionModeNamedBarrierCountINTEL) |
| 359 |
.add(NBarrierCnt) |
| 360 |
.done(); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
} // namespace SPIRV |
| 366 |
|
| 367 |
INITIALIZE_PASS(PreprocessMetadataLegacy, "preprocess-metadata", |
| 368 |
"Transform LLVM IR metadata to SPIR-V metadata format", false, |
| 369 |
false) |
| 370 |
|
| 371 |
ModulePass *llvm::createPreprocessMetadataLegacy() { |
| 372 |
return new PreprocessMetadataLegacy(); |
| 373 |
} |