alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 1 | // Copyright 2018 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 "clang/Basic/TargetInfo.h" |
| 16 | #include "clang/CodeGen/CodeGenAction.h" |
| 17 | #include "clang/Frontend/CompilerInstance.h" |
| 18 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 19 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 20 | #include "clang/Lex/PreprocessorOptions.h" |
| 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | #include "llvm/IR/LegacyPassManager.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Verifier.h" |
alan-baker | 0e64a59 | 2019-11-18 13:36:25 -0500 | [diff] [blame] | 25 | #include "llvm/InitializePasses.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 26 | #include "llvm/LinkAllPasses.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 27 | #include "llvm/Support/Allocator.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 29 | #include "llvm/Support/ErrorOr.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 31 | #include "llvm/Support/StringSaver.h" |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 32 | #include "llvm/Support/ToolOutputFile.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 35 | |
Kévin Petit | 38c5248 | 2019-05-07 20:28:00 +0800 | [diff] [blame] | 36 | #include "clspv/AddressSpace.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 37 | #include "clspv/DescriptorMap.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 38 | #include "clspv/Option.h" |
| 39 | #include "clspv/Passes.h" |
| 40 | #include "clspv/opencl_builtins_header.h" |
| 41 | |
| 42 | #include "FrontendPlugin.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 43 | #include "Passes.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 44 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 45 | #include <cassert> |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 46 | #include <numeric> |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 47 | #include <sstream> |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 48 | #include <string> |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 49 | |
| 50 | using namespace clang; |
| 51 | |
| 52 | namespace { |
| 53 | // This registration must be located in the same file as the execution of the |
| 54 | // action. |
| 55 | static FrontendPluginRegistry::Add<clspv::ExtraValidationASTAction> |
| 56 | X("extra-validation", |
| 57 | "Perform extra validation on OpenCL C when targeting Vulkan"); |
| 58 | |
| 59 | static llvm::cl::opt<bool> cl_single_precision_constants( |
| 60 | "cl-single-precision-constant", llvm::cl::init(false), |
| 61 | llvm::cl::desc("Treat double precision floating-point constant as single " |
| 62 | "precision constant.")); |
| 63 | |
| 64 | static llvm::cl::opt<bool> cl_denorms_are_zero( |
| 65 | "cl-denorms-are-zero", llvm::cl::init(false), |
| 66 | llvm::cl::desc("If specified, denormalized floating point numbers may be " |
| 67 | "flushed to zero.")); |
| 68 | |
| 69 | static llvm::cl::opt<bool> cl_fp32_correctly_rounded_divide_sqrt( |
| 70 | "cl-fp32-correctly-rounded-divide-sqrt", llvm::cl::init(false), |
| 71 | llvm::cl::desc("Single precision floating-point divide (x/y and 1/x) and " |
| 72 | "sqrt used are correctly rounded.")); |
| 73 | |
| 74 | static llvm::cl::opt<bool> |
| 75 | cl_opt_disable("cl-opt-disable", llvm::cl::init(false), |
| 76 | llvm::cl::desc("This option disables all optimizations. The " |
| 77 | "default is optimizations are enabled.")); |
| 78 | |
| 79 | static llvm::cl::opt<bool> cl_mad_enable( |
| 80 | "cl-mad-enable", llvm::cl::init(false), |
| 81 | llvm::cl::desc("Allow a * b + c to be replaced by a mad. The mad computes " |
| 82 | "a * b + c with reduced accuracy.")); |
| 83 | |
| 84 | static llvm::cl::opt<bool> cl_no_signed_zeros( |
| 85 | "cl-no-signed-zeros", llvm::cl::init(false), |
| 86 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that " |
| 87 | "ignore the signedness of zero.")); |
| 88 | |
| 89 | static llvm::cl::opt<bool> cl_unsafe_math_optimizations( |
| 90 | "cl-unsafe-math-optimizations", llvm::cl::init(false), |
| 91 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that (a) " |
| 92 | "assume that arguments and results are valid, (b) may " |
| 93 | "violate IEEE 754 standard and (c) may violate the OpenCL " |
| 94 | "numerical compliance requirements. This option includes " |
| 95 | "the -cl-no-signed-zeros and -cl-mad-enable options.")); |
| 96 | |
| 97 | static llvm::cl::opt<bool> cl_finite_math_only( |
| 98 | "cl-finite-math-only", llvm::cl::init(false), |
| 99 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that " |
| 100 | "assume that arguments and results are not NaNs or INFs.")); |
| 101 | |
| 102 | static llvm::cl::opt<bool> cl_fast_relaxed_math( |
| 103 | "cl-fast-relaxed-math", llvm::cl::init(false), |
| 104 | llvm::cl::desc("This option causes the preprocessor macro " |
| 105 | "__FAST_RELAXED_MATH__ to be defined. Sets the optimization " |
| 106 | "options -cl-finite-math-only and " |
| 107 | "-cl-unsafe-math-optimizations.")); |
| 108 | |
| 109 | static llvm::cl::list<std::string> |
| 110 | Includes(llvm::cl::Prefix, "I", |
| 111 | llvm::cl::desc("Add a directory to the list of directories " |
| 112 | "to be searched for header files."), |
| 113 | llvm::cl::ZeroOrMore, llvm::cl::value_desc("include path")); |
| 114 | |
| 115 | static llvm::cl::list<std::string> |
| 116 | Defines(llvm::cl::Prefix, "D", |
| 117 | llvm::cl::desc("Define a #define directive."), llvm::cl::ZeroOrMore, |
| 118 | llvm::cl::value_desc("define")); |
| 119 | |
| 120 | static llvm::cl::opt<std::string> |
| 121 | InputFilename(llvm::cl::Positional, llvm::cl::desc("<input .cl file>"), |
| 122 | llvm::cl::init("-")); |
| 123 | |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 124 | static llvm::cl::opt<clang::Language> InputLanguage( |
| 125 | "x", llvm::cl::desc("Select input type"), |
| 126 | llvm::cl::init(clang::Language::OpenCL), |
| 127 | llvm::cl::values(clEnumValN(clang::Language::OpenCL, "cl", "OpenCL source"), |
| 128 | clEnumValN(clang::Language::LLVM_IR, "ir", "LLVM IR"))); |
| 129 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 130 | static llvm::cl::opt<std::string> |
| 131 | OutputFilename("o", llvm::cl::desc("Override output filename"), |
| 132 | llvm::cl::value_desc("filename")); |
| 133 | |
| 134 | static llvm::cl::opt<std::string> |
| 135 | DescriptorMapFilename("descriptormap", |
| 136 | llvm::cl::desc("Output file for descriptor map"), |
| 137 | llvm::cl::value_desc("filename")); |
| 138 | |
| 139 | static llvm::cl::opt<char> |
| 140 | OptimizationLevel(llvm::cl::Prefix, "O", llvm::cl::init('2'), |
| 141 | llvm::cl::desc("Optimization level to use"), |
| 142 | llvm::cl::value_desc("level")); |
| 143 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 144 | static llvm::cl::opt<std::string> OutputFormat( |
| 145 | "mfmt", llvm::cl::init(""), |
| 146 | llvm::cl::desc( |
| 147 | "Specify special output format. 'c' is as a C initializer list"), |
| 148 | llvm::cl::value_desc("format")); |
| 149 | |
| 150 | static llvm::cl::opt<std::string> |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 151 | SamplerMap("samplermap", llvm::cl::desc("DEPRECATED - Literal sampler map"), |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 152 | llvm::cl::value_desc("filename")); |
| 153 | |
| 154 | static llvm::cl::opt<bool> cluster_non_pointer_kernel_args( |
| 155 | "cluster-pod-kernel-args", llvm::cl::init(false), |
| 156 | llvm::cl::desc("Collect plain-old-data kernel arguments into a struct in " |
| 157 | "a single storage buffer, using a binding number after " |
| 158 | "other arguments. Use this to reduce storage buffer " |
| 159 | "descriptors.")); |
| 160 | |
| 161 | static llvm::cl::opt<bool> verify("verify", llvm::cl::init(false), |
| 162 | llvm::cl::desc("Verify diagnostic outputs")); |
| 163 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 164 | static llvm::cl::opt<bool> |
| 165 | IgnoreWarnings("w", llvm::cl::init(false), |
| 166 | llvm::cl::desc("Disable all warnings")); |
| 167 | |
| 168 | static llvm::cl::opt<bool> |
| 169 | WarningsAsErrors("Werror", llvm::cl::init(false), |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 170 | llvm::cl::desc("Turn warnings into errors")); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 171 | |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 172 | static llvm::cl::opt<std::string> IROutputFile( |
| 173 | "emit-ir", |
| 174 | llvm::cl::desc( |
| 175 | "Emit LLVM IR to the given file after parsing and stop compilation."), |
| 176 | llvm::cl::value_desc("filename")); |
| 177 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 178 | // Populates |SamplerMapEntries| with data from the input sampler map. Returns 0 |
| 179 | // if successful. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 180 | int ParseSamplerMap(const std::string &sampler_map, |
| 181 | llvm::SmallVectorImpl<std::pair<unsigned, std::string>> |
| 182 | *SamplerMapEntries) { |
| 183 | std::unique_ptr<llvm::MemoryBuffer> samplerMapBuffer(nullptr); |
| 184 | if (!sampler_map.empty()) { |
| 185 | // Parse the sampler map from the provided string. |
| 186 | samplerMapBuffer = llvm::MemoryBuffer::getMemBuffer(sampler_map); |
| 187 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 188 | clspv::Option::SetUseSamplerMap(true); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 189 | if (!SamplerMap.empty()) { |
| 190 | llvm::outs() << "Warning: -samplermap is ignored when the sampler map is " |
| 191 | "provided through a string.\n"; |
| 192 | } |
| 193 | } else if (!SamplerMap.empty()) { |
| 194 | // Parse the sampler map from the option provided file. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 195 | auto errorOrSamplerMapFile = |
| 196 | llvm::MemoryBuffer::getFile(SamplerMap.getValue()); |
| 197 | |
| 198 | // If there was an error in getting the sampler map file. |
| 199 | if (!errorOrSamplerMapFile) { |
| 200 | llvm::errs() << "Error: " << errorOrSamplerMapFile.getError().message() |
| 201 | << " '" << SamplerMap.getValue() << "'\n"; |
| 202 | return -1; |
| 203 | } |
| 204 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 205 | clspv::Option::SetUseSamplerMap(true); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 206 | samplerMapBuffer = std::move(errorOrSamplerMapFile.get()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 207 | if (0 == samplerMapBuffer->getBufferSize()) { |
| 208 | llvm::errs() << "Error: Sampler map was an empty file!\n"; |
| 209 | return -1; |
| 210 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 211 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 212 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 213 | if (clspv::Option::UseSamplerMap()) { |
| 214 | llvm::outs() |
| 215 | << "Warning: use of the sampler map is deprecated and unnecessary\n"; |
| 216 | } |
| 217 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 218 | // No sampler map to parse. |
| 219 | if (!samplerMapBuffer || 0 == samplerMapBuffer->getBufferSize()) |
| 220 | return 0; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 221 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 222 | llvm::SmallVector<llvm::StringRef, 3> samplerStrings; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 223 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 224 | // We need to keep track of the beginning of the current entry. |
| 225 | const char *b = samplerMapBuffer->getBufferStart(); |
| 226 | for (const char *i = b, *e = samplerMapBuffer->getBufferEnd();; i++) { |
| 227 | // If we have a separator between declarations. |
| 228 | if ((*i == '|') || (*i == ',') || (i == e)) { |
| 229 | if (i == b) { |
| 230 | llvm::errs() << "Error: Sampler map contained an empty entry!\n"; |
| 231 | return -1; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 232 | } |
| 233 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 234 | samplerStrings.push_back(llvm::StringRef(b, i - b).trim()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 235 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 236 | // And set b the next character after i. |
| 237 | b = i + 1; |
| 238 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 239 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 240 | // If we have a separator between declarations within a single sampler. |
| 241 | if ((*i == ',') || (i == e)) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 242 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 243 | clspv::version0::SamplerNormalizedCoords NormalizedCoord = |
| 244 | clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET; |
| 245 | clspv::version0::SamplerAddressingMode AddressingMode = |
| 246 | clspv::version0::CLK_ADDRESS_NOT_SET; |
| 247 | clspv::version0::SamplerFilterMode FilterMode = |
| 248 | clspv::version0::CLK_FILTER_NOT_SET; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 249 | |
| 250 | for (auto str : samplerStrings) { |
| 251 | if ("CLK_NORMALIZED_COORDS_FALSE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 252 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET != |
| 253 | NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 254 | llvm::errs() << "Error: Sampler map normalized coordinates was " |
| 255 | "previously set!\n"; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 256 | return -1; |
| 257 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 258 | NormalizedCoord = clspv::version0::CLK_NORMALIZED_COORDS_FALSE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 259 | } else if ("CLK_NORMALIZED_COORDS_TRUE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 260 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET != |
| 261 | NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 262 | llvm::errs() << "Error: Sampler map normalized coordinates was " |
| 263 | "previously set!\n"; |
| 264 | return -1; |
| 265 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 266 | NormalizedCoord = clspv::version0::CLK_NORMALIZED_COORDS_TRUE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 267 | } else if ("CLK_ADDRESS_NONE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 268 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 269 | llvm::errs() |
| 270 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 271 | return -1; |
| 272 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 273 | AddressingMode = clspv::version0::CLK_ADDRESS_NONE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 274 | } else if ("CLK_ADDRESS_CLAMP_TO_EDGE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 275 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 276 | llvm::errs() |
| 277 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 278 | return -1; |
| 279 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 280 | AddressingMode = clspv::version0::CLK_ADDRESS_CLAMP_TO_EDGE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 281 | } else if ("CLK_ADDRESS_CLAMP" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 282 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 283 | llvm::errs() |
| 284 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 285 | return -1; |
| 286 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 287 | AddressingMode = clspv::version0::CLK_ADDRESS_CLAMP; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 288 | } else if ("CLK_ADDRESS_MIRRORED_REPEAT" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 289 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 290 | llvm::errs() |
| 291 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 292 | return -1; |
| 293 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 294 | AddressingMode = clspv::version0::CLK_ADDRESS_MIRRORED_REPEAT; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 295 | } else if ("CLK_ADDRESS_REPEAT" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 296 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 297 | llvm::errs() |
| 298 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 299 | return -1; |
| 300 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 301 | AddressingMode = clspv::version0::CLK_ADDRESS_REPEAT; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 302 | } else if ("CLK_FILTER_NEAREST" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 303 | if (clspv::version0::CLK_FILTER_NOT_SET != FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 304 | llvm::errs() |
| 305 | << "Error: Sampler map filtering mode was previously set!\n"; |
| 306 | return -1; |
| 307 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 308 | FilterMode = clspv::version0::CLK_FILTER_NEAREST; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 309 | } else if ("CLK_FILTER_LINEAR" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 310 | if (clspv::version0::CLK_FILTER_NOT_SET != FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 311 | llvm::errs() |
| 312 | << "Error: Sampler map filtering mode was previously set!\n"; |
| 313 | return -1; |
| 314 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 315 | FilterMode = clspv::version0::CLK_FILTER_LINEAR; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 316 | } else { |
| 317 | llvm::errs() << "Error: Unknown sampler string '" << str |
| 318 | << "' found!\n"; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 319 | return -1; |
| 320 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 321 | } |
| 322 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 323 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET == NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 324 | llvm::errs() << "Error: Sampler map entry did not contain normalized " |
| 325 | "coordinates entry!\n"; |
| 326 | return -1; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 327 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 328 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 329 | if (clspv::version0::CLK_ADDRESS_NOT_SET == AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 330 | llvm::errs() << "Error: Sampler map entry did not contain addressing " |
| 331 | "mode entry!\n"; |
| 332 | return -1; |
| 333 | } |
| 334 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame^] | 335 | if (clspv::version0::CLK_FILTER_NOT_SET == FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 336 | llvm::errs() |
| 337 | << "Error: Sampler map entry did not contain filer mode entry!\n"; |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | // Generate an equivalent expression in string form. Sort the |
| 342 | // strings to get a canonical ordering. |
| 343 | std::sort(samplerStrings.begin(), samplerStrings.end(), |
| 344 | std::less<StringRef>()); |
| 345 | const auto samplerExpr = std::accumulate( |
| 346 | samplerStrings.begin(), samplerStrings.end(), std::string(), |
| 347 | [](std::string left, std::string right) { |
| 348 | return left + std::string(left.empty() ? "" : "|") + right; |
| 349 | }); |
| 350 | |
| 351 | // SamplerMapEntries->push_back(std::make_pair( |
| 352 | // NormalizedCoord | AddressingMode | FilterMode, samplerExpr)); |
| 353 | SamplerMapEntries->emplace_back( |
| 354 | NormalizedCoord | AddressingMode | FilterMode, samplerExpr); |
| 355 | |
| 356 | // And reset the sampler strings for the next sampler in the map. |
| 357 | samplerStrings.clear(); |
| 358 | } |
| 359 | |
| 360 | // And lastly, if we are at the end of the file |
| 361 | if (i == e) { |
| 362 | break; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | // Sets |instance|'s options for compiling. Returns 0 if successful. |
| 370 | int SetCompilerInstanceOptions(CompilerInstance &instance, |
| 371 | const llvm::StringRef &overiddenInputFilename, |
| 372 | const clang::FrontendInputFile &kernelFile, |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 373 | const std::string &program, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 374 | llvm::raw_string_ostream *diagnosticsStream) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 375 | std::unique_ptr<llvm::MemoryBuffer> memory_buffer(nullptr); |
| 376 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> errorOrInputFile(nullptr); |
| 377 | if (program.empty()) { |
| 378 | auto errorOrInputFile = |
| 379 | llvm::MemoryBuffer::getFileOrSTDIN(InputFilename.getValue()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 380 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 381 | // If there was an error in getting the input file. |
| 382 | if (!errorOrInputFile) { |
| 383 | llvm::errs() << "Error: " << errorOrInputFile.getError().message() << " '" |
| 384 | << InputFilename.getValue() << "'\n"; |
| 385 | return -1; |
| 386 | } |
| 387 | memory_buffer.reset(errorOrInputFile.get().release()); |
| 388 | } else { |
| 389 | memory_buffer = llvm::MemoryBuffer::getMemBuffer(program.c_str(), |
| 390 | overiddenInputFilename); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 391 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 392 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 393 | if (verify) { |
| 394 | instance.getDiagnosticOpts().VerifyDiagnostics = true; |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 395 | instance.getDiagnosticOpts().VerifyPrefixes.push_back("expected"); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 398 | clang::LangStandard::Kind standard; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 399 | switch (clspv::Option::Language()) { |
| 400 | case clspv::Option::SourceLanguage::OpenCL_C_10: |
| 401 | standard = clang::LangStandard::lang_opencl10; |
| 402 | break; |
| 403 | case clspv::Option::SourceLanguage::OpenCL_C_11: |
| 404 | standard = clang::LangStandard::lang_opencl11; |
| 405 | break; |
| 406 | case clspv::Option::SourceLanguage::OpenCL_C_12: |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 407 | standard = clang::LangStandard::lang_opencl12; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 408 | break; |
| 409 | case clspv::Option::SourceLanguage::OpenCL_C_20: |
| 410 | standard = clang::LangStandard::lang_opencl20; |
| 411 | break; |
| 412 | case clspv::Option::SourceLanguage::OpenCL_CPP: |
| 413 | standard = clang::LangStandard::lang_openclcpp; |
| 414 | break; |
| 415 | default: |
| 416 | llvm_unreachable("Unknown source language"); |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 417 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 418 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 419 | instance.getLangOpts().C99 = true; |
| 420 | instance.getLangOpts().RTTI = false; |
| 421 | instance.getLangOpts().RTTIData = false; |
| 422 | instance.getLangOpts().MathErrno = false; |
| 423 | instance.getLangOpts().Optimize = false; |
| 424 | instance.getLangOpts().NoBuiltin = true; |
| 425 | instance.getLangOpts().ModulesSearchAll = false; |
| 426 | instance.getLangOpts().SinglePrecisionConstants = true; |
| 427 | instance.getCodeGenOpts().StackRealignment = true; |
| 428 | instance.getCodeGenOpts().SimplifyLibCalls = false; |
| 429 | instance.getCodeGenOpts().EmitOpenCLArgMetadata = false; |
| 430 | instance.getCodeGenOpts().DisableO0ImplyOptNone = true; |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 431 | instance.getDiagnosticOpts().IgnoreWarnings = IgnoreWarnings; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 432 | |
| 433 | instance.getLangOpts().SinglePrecisionConstants = |
| 434 | cl_single_precision_constants; |
| 435 | // cl_denorms_are_zero ignored for now! |
| 436 | // cl_fp32_correctly_rounded_divide_sqrt ignored for now! |
| 437 | instance.getCodeGenOpts().LessPreciseFPMAD = |
| 438 | cl_mad_enable || cl_unsafe_math_optimizations; |
| 439 | // cl_no_signed_zeros ignored for now! |
| 440 | instance.getCodeGenOpts().UnsafeFPMath = |
| 441 | cl_unsafe_math_optimizations || cl_fast_relaxed_math; |
| 442 | instance.getLangOpts().FiniteMathOnly = |
| 443 | cl_finite_math_only || cl_fast_relaxed_math; |
| 444 | instance.getLangOpts().FastRelaxedMath = cl_fast_relaxed_math; |
| 445 | |
| 446 | // Preprocessor options |
Kévin Petit | a624c0c | 2019-05-07 20:27:43 +0800 | [diff] [blame] | 447 | if (!clspv::Option::ImageSupport()) { |
| 448 | instance.getPreprocessorOpts().addMacroUndef("__IMAGE_SUPPORT__"); |
| 449 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 450 | if (cl_fast_relaxed_math) { |
| 451 | instance.getPreprocessorOpts().addMacroDef("__FAST_RELAXED_MATH__"); |
| 452 | } |
| 453 | |
| 454 | for (auto define : Defines) { |
| 455 | instance.getPreprocessorOpts().addMacroDef(define); |
| 456 | } |
| 457 | |
| 458 | // Header search options |
| 459 | for (auto include : Includes) { |
| 460 | instance.getHeaderSearchOpts().AddPath(include, clang::frontend::After, |
| 461 | false, false); |
| 462 | } |
| 463 | |
| 464 | // We always compile on opt 0 so we preserve as much debug information about |
| 465 | // the source as possible. We'll run optimization later, once we've had a |
| 466 | // chance to view the unoptimal code first |
| 467 | instance.getCodeGenOpts().OptimizationLevel = 0; |
| 468 | |
| 469 | // Debug information is disabled temporarily to call instruction. |
| 470 | #if 0 |
| 471 | instance.getCodeGenOpts().setDebugInfo(clang::codegenoptions::FullDebugInfo); |
| 472 | #endif |
| 473 | |
| 474 | // We use the 32-bit pointer-width SPIR triple |
| 475 | llvm::Triple triple("spir-unknown-unknown"); |
| 476 | |
| 477 | instance.getInvocation().setLangDefaults( |
alan-baker | d354f1a | 2019-08-06 15:41:55 -0400 | [diff] [blame] | 478 | instance.getLangOpts(), clang::InputKind(clang::Language::OpenCL), triple, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 479 | instance.getPreprocessorOpts(), standard); |
| 480 | |
| 481 | // Override the C99 inline semantics to accommodate for more OpenCL C |
| 482 | // programs in the wild. |
| 483 | instance.getLangOpts().GNUInline = true; |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 484 | |
| 485 | // Set up diagnostics |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 486 | instance.createDiagnostics( |
| 487 | new clang::TextDiagnosticPrinter(*diagnosticsStream, |
| 488 | &instance.getDiagnosticOpts()), |
| 489 | true); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 490 | instance.getDiagnostics().setWarningsAsErrors(WarningsAsErrors); |
| 491 | instance.getDiagnostics().setEnableAllWarnings(true); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 492 | |
| 493 | instance.getTargetOpts().Triple = triple.str(); |
| 494 | |
| 495 | instance.getCodeGenOpts().MainFileName = overiddenInputFilename; |
| 496 | instance.getCodeGenOpts().PreserveVec3Type = true; |
| 497 | // Disable generation of lifetime intrinsic. |
| 498 | instance.getCodeGenOpts().DisableLifetimeMarkers = true; |
| 499 | instance.getFrontendOpts().Inputs.push_back(kernelFile); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 500 | instance.getPreprocessorOpts().addRemappedFile(overiddenInputFilename, |
| 501 | memory_buffer.release()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 502 | |
| 503 | struct OpenCLBuiltinMemoryBuffer final : public llvm::MemoryBuffer { |
| 504 | OpenCLBuiltinMemoryBuffer(const void *data, uint64_t data_length) { |
| 505 | const char *dataCasted = reinterpret_cast<const char *>(data); |
| 506 | init(dataCasted, dataCasted + data_length, true); |
| 507 | } |
| 508 | |
| 509 | virtual llvm::MemoryBuffer::BufferKind getBufferKind() const override { |
| 510 | return llvm::MemoryBuffer::MemoryBuffer_Malloc; |
| 511 | } |
| 512 | |
| 513 | virtual ~OpenCLBuiltinMemoryBuffer() override {} |
| 514 | }; |
| 515 | |
| 516 | std::unique_ptr<llvm::MemoryBuffer> openCLBuiltinMemoryBuffer( |
| 517 | new OpenCLBuiltinMemoryBuffer(opencl_builtins_header_data, |
| 518 | opencl_builtins_header_size - 1)); |
| 519 | |
| 520 | instance.getPreprocessorOpts().Includes.push_back("openclc.h"); |
| 521 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 522 | std::unique_ptr<llvm::MemoryBuffer> openCLBaseBuiltinMemoryBuffer( |
| 523 | new OpenCLBuiltinMemoryBuffer(opencl_base_builtins_header_data, |
| 524 | opencl_base_builtins_header_size - 1)); |
| 525 | |
| 526 | instance.getPreprocessorOpts().Includes.push_back("opencl-c-base.h"); |
| 527 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 528 | // Add the VULKAN macro. |
| 529 | instance.getPreprocessorOpts().addMacroDef("VULKAN=100"); |
| 530 | |
| 531 | // Add the __OPENCL_VERSION__ macro. |
| 532 | instance.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=120"); |
| 533 | |
| 534 | instance.setTarget(clang::TargetInfo::CreateTargetInfo( |
| 535 | instance.getDiagnostics(), |
| 536 | std::make_shared<clang::TargetOptions>(instance.getTargetOpts()))); |
| 537 | |
| 538 | instance.createFileManager(); |
| 539 | instance.createSourceManager(instance.getFileManager()); |
| 540 | |
| 541 | #ifdef _MSC_VER |
| 542 | std::string includePrefix("include\\"); |
| 543 | #else |
| 544 | std::string includePrefix("include/"); |
| 545 | #endif |
| 546 | |
| 547 | auto entry = instance.getFileManager().getVirtualFile( |
| 548 | includePrefix + "openclc.h", openCLBuiltinMemoryBuffer->getBufferSize(), |
| 549 | 0); |
| 550 | |
| 551 | instance.getSourceManager().overrideFileContents( |
| 552 | entry, std::move(openCLBuiltinMemoryBuffer)); |
| 553 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 554 | auto base_entry = instance.getFileManager().getVirtualFile( |
| 555 | includePrefix + "opencl-c-base.h", |
| 556 | openCLBaseBuiltinMemoryBuffer->getBufferSize(), 0); |
| 557 | |
| 558 | instance.getSourceManager().overrideFileContents( |
| 559 | base_entry, std::move(openCLBaseBuiltinMemoryBuffer)); |
| 560 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 564 | // Populates |pm| with necessary passes to optimize and legalize the IR. |
| 565 | int PopulatePassManager( |
| 566 | llvm::legacy::PassManager *pm, llvm::raw_svector_ostream *binaryStream, |
| 567 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries, |
| 568 | llvm::SmallVectorImpl<std::pair<unsigned, std::string>> |
| 569 | *SamplerMapEntries) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 570 | llvm::PassManagerBuilder pmBuilder; |
| 571 | |
| 572 | switch (OptimizationLevel) { |
| 573 | case '0': |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 574 | case '1': |
| 575 | case '2': |
| 576 | case '3': |
| 577 | case 's': |
| 578 | case 'z': |
| 579 | break; |
| 580 | default: |
| 581 | llvm::errs() << "Unknown optimization level -O" << OptimizationLevel |
| 582 | << " specified!\n"; |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | switch (OptimizationLevel) { |
| 587 | case '0': |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 588 | pmBuilder.OptLevel = 0; |
| 589 | break; |
| 590 | case '1': |
| 591 | pmBuilder.OptLevel = 1; |
| 592 | break; |
| 593 | case '2': |
| 594 | pmBuilder.OptLevel = 2; |
| 595 | break; |
| 596 | case '3': |
| 597 | pmBuilder.OptLevel = 3; |
| 598 | break; |
| 599 | case 's': |
| 600 | pmBuilder.SizeLevel = 1; |
| 601 | break; |
| 602 | case 'z': |
| 603 | pmBuilder.SizeLevel = 2; |
| 604 | break; |
| 605 | default: |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | pm->add(clspv::createZeroInitializeAllocasPass()); |
| 610 | pm->add(clspv::createDefineOpenCLWorkItemBuiltinsPass()); |
| 611 | |
| 612 | if (0 < pmBuilder.OptLevel) { |
| 613 | pm->add(clspv::createOpenCLInlinerPass()); |
| 614 | } |
| 615 | |
| 616 | pm->add(clspv::createUndoByvalPass()); |
| 617 | pm->add(clspv::createUndoSRetPass()); |
| 618 | if (cluster_non_pointer_kernel_args) { |
| 619 | pm->add(clspv::createClusterPodKernelArgumentsPass()); |
| 620 | } |
| 621 | pm->add(clspv::createReplaceOpenCLBuiltinPass()); |
| 622 | |
| 623 | // We need to run mem2reg and inst combine early because our |
| 624 | // createInlineFuncWithPointerBitCastArgPass pass cannot handle the pattern |
| 625 | // %1 = alloca i32 1 |
| 626 | // store <something> %1 |
| 627 | // %2 = bitcast float* %1 |
| 628 | // %3 = load float %2 |
| 629 | pm->add(llvm::createPromoteMemoryToRegisterPass()); |
| 630 | |
alan-baker | 1b13e8f | 2019-08-08 17:56:51 -0400 | [diff] [blame] | 631 | // Try to deal with pointer bitcasts early. This can prevent problems like |
| 632 | // issue #409 where LLVM is looser about access chain addressing than SPIR-V. |
| 633 | // This needs to happen before instcombine and after replacing OpenCL |
| 634 | // builtins. This run of the pass will not handle all pointer bitcasts that |
| 635 | // could be handled. It should be run again after other optimizations (e.g |
| 636 | // InlineFuncWithPointerBitCastArgPass). |
| 637 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 638 | pm->add(clspv::createReplacePointerBitcastPass()); |
| 639 | pm->add(llvm::createDeadCodeEliminationPass()); |
| 640 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 641 | // Hide loads from __constant address space away from instcombine. |
| 642 | // This prevents us from generating select between pointers-to-__constant. |
| 643 | // See https://github.com/google/clspv/issues/71 |
| 644 | pm->add(clspv::createHideConstantLoadsPass()); |
| 645 | |
| 646 | pm->add(llvm::createInstructionCombiningPass()); |
| 647 | |
| 648 | if (clspv::Option::InlineEntryPoints()) { |
| 649 | pm->add(clspv::createInlineEntryPointsPass()); |
| 650 | } else { |
| 651 | pm->add(clspv::createInlineFuncWithPointerBitCastArgPass()); |
| 652 | pm->add(clspv::createInlineFuncWithPointerToFunctionArgPass()); |
| 653 | pm->add(clspv::createInlineFuncWithSingleCallSitePass()); |
| 654 | } |
| 655 | |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 656 | if (clspv::Option::LanguageUsesGenericAddressSpace()) { |
Kévin Petit | 38c5248 | 2019-05-07 20:28:00 +0800 | [diff] [blame] | 657 | pm->add(llvm::createInferAddressSpacesPass(clspv::AddressSpace::Generic)); |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 658 | } |
| 659 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 660 | if (0 == pmBuilder.OptLevel) { |
| 661 | // Mem2Reg pass should be run early because O0 level optimization leaves |
| 662 | // redundant alloca, load and store instructions from function arguments. |
| 663 | // clspv needs to remove them ahead of transformation. |
| 664 | pm->add(llvm::createPromoteMemoryToRegisterPass()); |
| 665 | |
| 666 | // SROA pass is run because it will fold structs/unions that are problematic |
| 667 | // on Vulkan SPIR-V away. |
| 668 | pm->add(llvm::createSROAPass()); |
| 669 | |
| 670 | // InstructionCombining pass folds bitcast and gep instructions which are |
| 671 | // not supported by Vulkan SPIR-V. |
| 672 | pm->add(llvm::createInstructionCombiningPass()); |
| 673 | } |
| 674 | |
| 675 | // Now we add any of the LLVM optimizations we wanted |
| 676 | pmBuilder.populateModulePassManager(*pm); |
| 677 | |
| 678 | // Unhide loads from __constant address space. Undoes the action of |
| 679 | // HideConstantLoadsPass. |
| 680 | pm->add(clspv::createUnhideConstantLoadsPass()); |
| 681 | |
| 682 | pm->add(clspv::createFunctionInternalizerPass()); |
| 683 | pm->add(clspv::createReplaceLLVMIntrinsicsPass()); |
| 684 | pm->add(clspv::createUndoBoolPass()); |
| 685 | pm->add(clspv::createUndoTruncatedSwitchConditionPass()); |
| 686 | pm->add(llvm::createStructurizeCFGPass(false)); |
alan-baker | 3fa76d9 | 2018-11-12 14:54:40 -0500 | [diff] [blame] | 687 | // Must be run after structurize cfg. |
alan-baker | 9580aef | 2020-01-07 22:31:48 -0500 | [diff] [blame] | 688 | pm->add(clspv::createFixupStructuredCFGPass()); |
| 689 | // Must be run after structured cfg fixup. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 690 | pm->add(clspv::createReorderBasicBlocksPass()); |
| 691 | pm->add(clspv::createUndoGetElementPtrConstantExprPass()); |
| 692 | pm->add(clspv::createSplatArgPass()); |
| 693 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 694 | pm->add(clspv::createReplacePointerBitcastPass()); |
| 695 | |
| 696 | pm->add(clspv::createUndoTranslateSamplerFoldPass()); |
| 697 | |
| 698 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 699 | pm->add(clspv::createClusterModuleScopeConstantVars()); |
| 700 | } |
| 701 | |
| 702 | pm->add(clspv::createShareModuleScopeVariablesPass()); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 703 | // Specialize images before assigning descriptors to disambiguate the various |
| 704 | // types. |
| 705 | pm->add(clspv::createSpecializeImageTypesPass()); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 706 | // This should be run after LLVM and OpenCL intrinsics are replaced. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 707 | pm->add(clspv::createAllocateDescriptorsPass(*SamplerMapEntries)); |
| 708 | pm->add(llvm::createVerifierPass()); |
| 709 | pm->add(clspv::createDirectResourceAccessPass()); |
| 710 | // Replacing pointer bitcasts can leave some trivial GEPs |
| 711 | // that are easy to remove. Also replace GEPs of GEPS |
| 712 | // left by replacing indirect buffer accesses. |
| 713 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
alan-baker | 4217b32 | 2019-03-06 08:56:12 -0500 | [diff] [blame] | 714 | // Run after DRA to clean up parameters and help reduce the need for variable |
| 715 | // pointers. |
| 716 | pm->add(clspv::createRemoveUnusedArgumentsPass()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 717 | |
| 718 | pm->add(clspv::createSplatSelectConditionPass()); |
| 719 | pm->add(clspv::createSignedCompareFixupPass()); |
| 720 | // This pass generates insertions that need to be rewritten. |
| 721 | pm->add(clspv::createScalarizePass()); |
| 722 | pm->add(clspv::createRewriteInsertsPass()); |
alan-baker | a71f193 | 2019-04-11 11:04:34 -0400 | [diff] [blame] | 723 | // UBO Transformations |
| 724 | if (clspv::Option::ConstantArgsInUniformBuffer() && |
| 725 | !clspv::Option::InlineEntryPoints()) { |
| 726 | // MultiVersionUBOFunctionsPass will examine non-kernel functions with UBO |
| 727 | // arguments and either multi-version them as necessary or inline them if |
| 728 | // multi-versioning cannot be accomplished. |
| 729 | pm->add(clspv::createMultiVersionUBOFunctionsPass()); |
| 730 | // Cleanup passes. |
| 731 | // Specialization can blindly generate GEP chains that are easily cleaned up |
| 732 | // by SimplifyPointerBitcastPass. |
| 733 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 734 | // RemoveUnusedArgumentsPass removes the actual UBO arguments that were |
| 735 | // problematic to begin with now that they have no uses. |
| 736 | pm->add(clspv::createRemoveUnusedArgumentsPass()); |
| 737 | // DCE cleans up callers of the specialized functions. |
| 738 | pm->add(llvm::createDeadCodeEliminationPass()); |
| 739 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 740 | // This pass mucks with types to point where you shouldn't rely on DataLayout |
| 741 | // anymore so leave this right before SPIR-V generation. |
| 742 | pm->add(clspv::createUBOTypeTransformPass()); |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 743 | pm->add(clspv::createSPIRVProducerPass(*binaryStream, descriptor_map_entries, |
| 744 | *SamplerMapEntries, |
| 745 | OutputFormat == "c")); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 746 | |
| 747 | return 0; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 748 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 749 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 750 | int ParseOptions(const int argc, const char *const argv[]) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 751 | // We need to change how one of the called passes works by spoofing |
| 752 | // ParseCommandLineOptions with the specific option. |
| 753 | const int llvmArgc = 2; |
| 754 | const char *llvmArgv[llvmArgc] = { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 755 | argv[0], |
| 756 | "-simplifycfg-sink-common=false", |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 757 | }; |
| 758 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 759 | llvm::cl::ResetAllOptionOccurrences(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 760 | llvm::cl::ParseCommandLineOptions(llvmArgc, llvmArgv); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 761 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 762 | |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 763 | if (clspv::Option::LanguageUsesGenericAddressSpace() && |
| 764 | !clspv::Option::InlineEntryPoints()) { |
| 765 | llvm::errs() << "cannot compile languages that use the generic address " |
| 766 | "space (e.g. CLC++, CL2.0) without -inline-entry-points\n"; |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 767 | return -1; |
| 768 | } |
| 769 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 770 | return 0; |
| 771 | } |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 772 | |
| 773 | int GenerateIRFile(llvm::legacy::PassManager *pm, llvm::Module &module, |
| 774 | std::string output) { |
| 775 | std::error_code ec; |
| 776 | std::unique_ptr<llvm::ToolOutputFile> out( |
| 777 | new llvm::ToolOutputFile(output, ec, llvm::sys::fs::F_None)); |
| 778 | if (ec) { |
| 779 | llvm::errs() << output << ": " << ec.message() << '\n'; |
| 780 | return -1; |
| 781 | } |
| 782 | pm->add(llvm::createPrintModulePass(out->os(), "", false)); |
| 783 | pm->run(module); |
| 784 | out->keep(); |
| 785 | return 0; |
| 786 | } |
| 787 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 788 | } // namespace |
| 789 | |
| 790 | namespace clspv { |
| 791 | int Compile(const int argc, const char *const argv[]) { |
| 792 | |
| 793 | if (auto error = ParseOptions(argc, argv)) |
| 794 | return error; |
| 795 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 796 | llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 797 | if (auto error = ParseSamplerMap("", &SamplerMapEntries)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 798 | return error; |
| 799 | |
| 800 | // if no output file was provided, use a default |
| 801 | llvm::StringRef overiddenInputFilename = InputFilename.getValue(); |
| 802 | |
| 803 | // If we are reading our input file from stdin. |
| 804 | if ("-" == InputFilename) { |
| 805 | // We need to overwrite the file name we use. |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 806 | switch (InputLanguage) { |
| 807 | case clang::Language::OpenCL: |
| 808 | overiddenInputFilename = "stdin.cl"; |
| 809 | break; |
| 810 | case clang::Language::LLVM_IR: |
| 811 | overiddenInputFilename = "stdin.ll"; |
| 812 | break; |
alan-baker | 31298a6 | 2019-10-07 13:24:30 -0400 | [diff] [blame] | 813 | default: |
| 814 | // Default to fix compiler warnings/errors. Option parsing will reject a |
| 815 | // bad enum value for the option so there is no need for a message. |
| 816 | return -1; |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 817 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | clang::CompilerInstance instance; |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 821 | clang::FrontendInputFile kernelFile(overiddenInputFilename, |
| 822 | clang::InputKind(InputLanguage)); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 823 | std::string log; |
| 824 | llvm::raw_string_ostream diagnosticsStream(log); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 825 | if (auto error = SetCompilerInstanceOptions( |
| 826 | instance, overiddenInputFilename, kernelFile, "", &diagnosticsStream)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 827 | return error; |
| 828 | |
| 829 | // Parse. |
| 830 | llvm::LLVMContext context; |
| 831 | clang::EmitLLVMOnlyAction action(&context); |
| 832 | |
| 833 | // Prepare the action for processing kernelFile |
| 834 | const bool success = action.BeginSourceFile(instance, kernelFile); |
| 835 | if (!success) { |
| 836 | return -1; |
| 837 | } |
| 838 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 839 | auto result = action.Execute(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 840 | action.EndSourceFile(); |
| 841 | |
| 842 | clang::DiagnosticConsumer *const consumer = |
| 843 | instance.getDiagnostics().getClient(); |
| 844 | consumer->finish(); |
| 845 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 846 | auto num_warnings = consumer->getNumWarnings(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 847 | auto num_errors = consumer->getNumErrors(); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 848 | if ((num_errors > 0) || (num_warnings > 0)) { |
| 849 | llvm::errs() << log; |
| 850 | } |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 851 | if (result || num_errors > 0) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 852 | return -1; |
| 853 | } |
| 854 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 855 | // Don't run the passes or produce any output in verify mode. |
| 856 | // Clang doesn't always produce a valid module. |
| 857 | if (verify) { |
| 858 | return 0; |
| 859 | } |
| 860 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 861 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 862 | llvm::initializeCore(Registry); |
| 863 | llvm::initializeScalarOpts(Registry); |
Diego Novillo | 1fcff72 | 2019-05-07 13:45:53 -0400 | [diff] [blame] | 864 | llvm::initializeClspvPasses(Registry); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 865 | |
| 866 | std::unique_ptr<llvm::Module> module(action.takeModule()); |
| 867 | |
| 868 | // Optimize. |
| 869 | // Create a memory buffer for temporarily writing the result. |
| 870 | SmallVector<char, 10000> binary; |
| 871 | llvm::raw_svector_ostream binaryStream(binary); |
| 872 | std::string descriptor_map; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 873 | llvm::legacy::PassManager pm; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 874 | std::vector<version0::DescriptorMapEntry> descriptor_map_entries; |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 875 | |
| 876 | // If --emit-ir was requested, emit the initial LLVM IR and stop compilation. |
| 877 | if (!IROutputFile.empty()) { |
| 878 | return GenerateIRFile(&pm, *module, IROutputFile); |
| 879 | } |
| 880 | |
| 881 | // Otherwise, populate the pass manager and run the regular passes. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 882 | if (auto error = PopulatePassManager( |
| 883 | &pm, &binaryStream, &descriptor_map_entries, &SamplerMapEntries)) |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 884 | return error; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 885 | pm.run(*module); |
| 886 | |
| 887 | // Write outputs |
| 888 | |
| 889 | // Write the descriptor map, if requested. |
| 890 | std::error_code error; |
| 891 | if (!DescriptorMapFilename.empty()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 892 | llvm::raw_fd_ostream descriptor_map_out_fd( |
| 893 | DescriptorMapFilename, error, llvm::sys::fs::CD_CreateAlways, |
| 894 | llvm::sys::fs::FA_Write, llvm::sys::fs::F_Text); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 895 | if (error) { |
| 896 | llvm::errs() << "Unable to open descriptor map file '" |
| 897 | << DescriptorMapFilename << "': " << error.message() << '\n'; |
| 898 | return -1; |
| 899 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 900 | std::string descriptor_map_string; |
| 901 | std::ostringstream str(descriptor_map_string); |
| 902 | for (const auto &entry : descriptor_map_entries) { |
| 903 | str << entry << "\n"; |
| 904 | } |
| 905 | descriptor_map_out_fd << str.str(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 906 | descriptor_map_out_fd.close(); |
| 907 | } |
| 908 | |
| 909 | // Write the resulting binary. |
| 910 | // Wait until now to try writing the file so that we only write it on |
| 911 | // successful compilation. |
| 912 | if (OutputFilename.empty()) { |
Kévin Petit | e478690 | 2019-04-02 21:51:47 +0100 | [diff] [blame] | 913 | if (OutputFormat == "c") { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 914 | OutputFilename = "a.spvinc"; |
| 915 | } else { |
| 916 | OutputFilename = "a.spv"; |
| 917 | } |
| 918 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 919 | llvm::raw_fd_ostream outStream(OutputFilename, error, |
| 920 | llvm::sys::fs::FA_Write); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 921 | |
| 922 | if (error) { |
| 923 | llvm::errs() << "Unable to open output file '" << OutputFilename |
| 924 | << "': " << error.message() << '\n'; |
| 925 | return -1; |
| 926 | } |
| 927 | outStream << binaryStream.str(); |
| 928 | |
| 929 | return 0; |
| 930 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 931 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 932 | int CompileFromSourceString( |
| 933 | const std::string &program, const std::string &sampler_map, |
| 934 | const std::string &options, std::vector<uint32_t> *output_binary, |
| 935 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 936 | |
| 937 | llvm::SmallVector<const char *, 20> argv; |
| 938 | llvm::BumpPtrAllocator A; |
| 939 | llvm::StringSaver Saver(A); |
| 940 | argv.push_back(Saver.save("clspv").data()); |
| 941 | llvm::cl::TokenizeGNUCommandLine(options, Saver, argv); |
| 942 | int argc = static_cast<int>(argv.size()); |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 943 | |
| 944 | if (auto error = ParseOptions(argc, &argv[0])) |
| 945 | return error; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 946 | |
| 947 | llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries; |
| 948 | if (auto error = ParseSamplerMap(sampler_map, &SamplerMapEntries)) |
| 949 | return error; |
| 950 | |
| 951 | InputFilename = "source.cl"; |
| 952 | llvm::StringRef overiddenInputFilename = InputFilename.getValue(); |
| 953 | |
| 954 | clang::CompilerInstance instance; |
alan-baker | d354f1a | 2019-08-06 15:41:55 -0400 | [diff] [blame] | 955 | clang::FrontendInputFile kernelFile( |
| 956 | overiddenInputFilename, clang::InputKind(clang::Language::OpenCL)); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 957 | std::string log; |
| 958 | llvm::raw_string_ostream diagnosticsStream(log); |
| 959 | if (auto error = |
| 960 | SetCompilerInstanceOptions(instance, overiddenInputFilename, |
| 961 | kernelFile, program, &diagnosticsStream)) |
| 962 | return error; |
| 963 | |
| 964 | // Parse. |
| 965 | llvm::LLVMContext context; |
| 966 | clang::EmitLLVMOnlyAction action(&context); |
| 967 | |
| 968 | // Prepare the action for processing kernelFile |
| 969 | const bool success = action.BeginSourceFile(instance, kernelFile); |
| 970 | if (!success) { |
| 971 | return -1; |
| 972 | } |
| 973 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 974 | auto result = action.Execute(); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 975 | action.EndSourceFile(); |
| 976 | |
| 977 | clang::DiagnosticConsumer *const consumer = |
| 978 | instance.getDiagnostics().getClient(); |
| 979 | consumer->finish(); |
| 980 | |
| 981 | auto num_errors = consumer->getNumErrors(); |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 982 | if (result || num_errors > 0) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 983 | llvm::errs() << log << "\n"; |
| 984 | return -1; |
| 985 | } |
| 986 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 987 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 988 | llvm::initializeCore(Registry); |
| 989 | llvm::initializeScalarOpts(Registry); |
Diego Novillo | 1fcff72 | 2019-05-07 13:45:53 -0400 | [diff] [blame] | 990 | llvm::initializeClspvPasses(Registry); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 991 | |
| 992 | std::unique_ptr<llvm::Module> module(action.takeModule()); |
| 993 | |
| 994 | // Optimize. |
| 995 | // Create a memory buffer for temporarily writing the result. |
| 996 | SmallVector<char, 10000> binary; |
| 997 | llvm::raw_svector_ostream binaryStream(binary); |
| 998 | std::string descriptor_map; |
| 999 | llvm::legacy::PassManager pm; |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1000 | if (auto error = PopulatePassManager( |
| 1001 | &pm, &binaryStream, descriptor_map_entries, &SamplerMapEntries)) |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1002 | return error; |
| 1003 | pm.run(*module); |
| 1004 | |
| 1005 | // Write outputs |
| 1006 | |
| 1007 | // Write the descriptor map. This is required. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1008 | assert(descriptor_map_entries && |
| 1009 | "Valid descriptor map container is required."); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1010 | if (!DescriptorMapFilename.empty()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1011 | llvm::errs() << "Warning: -descriptormap is ignored descriptor map " |
| 1012 | "container is provided.\n"; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // Write the resulting binary. |
| 1016 | // Wait until now to try writing the file so that we only write it on |
| 1017 | // successful compilation. |
| 1018 | assert(output_binary && "Valid binary container is required."); |
| 1019 | if (!OutputFilename.empty()) { |
| 1020 | llvm::outs() |
| 1021 | << "Warning: -o is ignored when binary container is provided.\n"; |
| 1022 | } |
| 1023 | output_binary->resize(binary.size() / 4); |
| 1024 | memcpy(output_binary->data(), binary.data(), binary.size()); |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 1028 | } // namespace clspv |