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