| 1 |
//===- SPIRVLowerOCLBlocks.cpp - OCL Utilities ----------------------------===// |
| 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) 2018 Intel Corporation. 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 Intel Corporation, 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 |
// SPIR-V specification doesn't allow function pointers, so SPIR-V translator |
| 36 |
// is designed to fail if a value with function type (except calls) occurs. |
| 37 |
// Currently there is only two cases, when function pointers are generating in |
| 38 |
// LLVM IR in OpenCL - block calls and device side enqueue built-in calls. |
| 39 |
// |
| 40 |
// In both cases values with function type used as intermediate representation |
| 41 |
// for block literal structure. |
| 42 |
// |
| 43 |
// In LLVM IR produced by clang, blocks are represented with the following |
| 44 |
// structure: |
| 45 |
// %struct.__opencl_block_literal_generic = type { i32, i32, i8 addrspace(4)* } |
| 46 |
// Pointers to block invoke functions are stored in the third field. Clang |
| 47 |
// replaces indirect function calls in all cases except if block is passed as a |
| 48 |
// function argument. Note that it is somewhat unclear if the OpenCL C spec |
| 49 |
// should allow passing blocks as function arguments. This pass is not supposed |
| 50 |
// to work correctly with such functions. |
| 51 |
// Clang though has to store function pointers to this structure. Purpose of |
| 52 |
// this pass is to replace store of function pointers(not allowed in SPIR-V) |
| 53 |
// with null pointers. |
| 54 |
// |
| 55 |
//===----------------------------------------------------------------------===// |
| 56 |
#define DEBUG_TYPE "spv-lower-ocl-blocks" |
| 57 |
|
| 58 |
#include "SPIRVLowerOCLBlocks.h" |
| 59 |
#include "SPIRVInternal.h" |
| 60 |
|
| 61 |
#include "llvm/IR/Module.h" |
| 62 |
#include "llvm/IR/PassManager.h" |
| 63 |
#include "llvm/Pass.h" |
| 64 |
#include "llvm/Support/Regex.h" |
| 65 |
|
| 66 |
using namespace llvm; |
| 67 |
|
| 68 |
namespace { |
| 69 |
|
| 70 |
static bool isBlockInvoke(Function &F) { |
| 71 |
static Regex BlockInvokeRegex("_block_invoke_?[0-9]*$"); |
| 72 |
return BlockInvokeRegex.match(F.getName()); |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace |
| 76 |
|
| 77 |
namespace SPIRV { |
| 78 |
|
| 79 |
bool SPIRVLowerOCLBlocksBase::runLowerOCLBlocks(Module &M) { |
| 80 |
bool Changed = false; |
| 81 |
for (Function &F : M) { |
| 82 |
if (!isBlockInvoke(F)) |
| 83 |
continue; |
| 84 |
for (User *U : F.users()) { |
| 85 |
if (!isa<Constant>(U)) |
| 86 |
continue; |
| 87 |
Constant *Null = Constant::getNullValue(U->getType()); |
| 88 |
if (U != Null) { |
| 89 |
U->replaceAllUsesWith(Null); |
| 90 |
Changed = true; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
return Changed; |
| 95 |
} |
| 96 |
|
| 97 |
llvm::PreservedAnalyses |
| 98 |
SPIRVLowerOCLBlocksPass::run(llvm::Module &M, |
| 99 |
llvm::ModuleAnalysisManager &MAM) { |
| 100 |
return runLowerOCLBlocks(M) ? llvm::PreservedAnalyses::none() |
| 101 |
: llvm::PreservedAnalyses::all(); |
| 102 |
} |
| 103 |
|
| 104 |
char SPIRVLowerOCLBlocksLegacy::ID = 0; |
| 105 |
|
| 106 |
} // namespace SPIRV |
| 107 |
|
| 108 |
INITIALIZE_PASS(SPIRVLowerOCLBlocksLegacy, "spv-lower-ocl-blocks", |
| 109 |
"Remove function pointers originating from OpenCL blocks", |
| 110 |
false, false) |
| 111 |
|
| 112 |
llvm::ModulePass *llvm::createSPIRVLowerOCLBlocksLegacy() { |
| 113 |
return new SPIRVLowerOCLBlocksLegacy(); |
| 114 |
} |