BackendUtil.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/CodeGen/BackendUtil.h"
  10. #include "clang/Basic/Diagnostic.h"
  11. #include "clang/Basic/LangOptions.h"
  12. #include "clang/Basic/TargetOptions.h"
  13. #include "clang/Frontend/CodeGenOptions.h"
  14. #include "clang/Frontend/FrontendDiagnostic.h"
  15. #include "clang/Frontend/Utils.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/Analysis/TargetLibraryInfo.h"
  18. #include "llvm/Analysis/TargetTransformInfo.h"
  19. #include "llvm/Bitcode/BitcodeWriterPass.h"
  20. #include "llvm/CodeGen/RegAllocRegistry.h"
  21. #include "llvm/CodeGen/SchedulerRegistry.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/IRPrintingPasses.h"
  24. #include "llvm/IR/LegacyPassManager.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/IR/Verifier.h"
  27. #include "llvm/MC/SubtargetFeature.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/PrettyStackTrace.h"
  30. #include "llvm/Support/TargetRegistry.h"
  31. #include "llvm/Support/Timer.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. #include "llvm/Target/TargetOptions.h"
  35. #include "llvm/Target/TargetSubtargetInfo.h"
  36. #include "llvm/Transforms/IPO.h"
  37. #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  38. #include "llvm/Transforms/Instrumentation.h"
  39. #include "llvm/Transforms/ObjCARC.h"
  40. #include "llvm/Transforms/Scalar.h"
  41. #include "llvm/Transforms/Utils/SymbolRewriter.h"
  42. #include <memory>
  43. #include "dxc/HLSL/DxilGenerationPass.h" // HLSL Change
  44. #include "dxc/HLSL/HLMatrixLowerPass.h" // HLSL Change
  45. using namespace clang;
  46. using namespace llvm;
  47. namespace {
  48. class EmitAssemblyHelper {
  49. DiagnosticsEngine &Diags;
  50. const CodeGenOptions &CodeGenOpts;
  51. const clang::TargetOptions &TargetOpts;
  52. const LangOptions &LangOpts;
  53. Module *TheModule;
  54. // HLSL Changes Start
  55. std::string CodeGenPassesConfig;
  56. std::string PerModulePassesConfig;
  57. std::string PerFunctionPassesConfig;
  58. mutable llvm::raw_string_ostream CodeGenPassesConfigOS;
  59. mutable llvm::raw_string_ostream PerModulePassesConfigOS;
  60. mutable llvm::raw_string_ostream PerFunctionPassesConfigOS;
  61. // HLSL Changes End
  62. Timer CodeGenerationTime;
  63. mutable legacy::PassManager *CodeGenPasses;
  64. mutable legacy::PassManager *PerModulePasses;
  65. mutable legacy::FunctionPassManager *PerFunctionPasses;
  66. private:
  67. TargetIRAnalysis getTargetIRAnalysis() const {
  68. if (TM)
  69. return TM->getTargetIRAnalysis();
  70. return TargetIRAnalysis();
  71. }
  72. legacy::PassManager *getCodeGenPasses() const {
  73. if (!CodeGenPasses) {
  74. CodeGenPasses = new legacy::PassManager();
  75. CodeGenPasses->TrackPassOS = &CodeGenPassesConfigOS;
  76. CodeGenPasses->add(
  77. createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
  78. }
  79. return CodeGenPasses;
  80. }
  81. legacy::PassManager *getPerModulePasses() const {
  82. if (!PerModulePasses) {
  83. PerModulePasses = new legacy::PassManager();
  84. PerModulePasses->TrackPassOS = &PerModulePassesConfigOS;
  85. PerModulePasses->add(
  86. createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
  87. }
  88. return PerModulePasses;
  89. }
  90. legacy::FunctionPassManager *getPerFunctionPasses() const {
  91. if (!PerFunctionPasses) {
  92. PerFunctionPasses = new legacy::FunctionPassManager(TheModule);
  93. PerFunctionPasses->TrackPassOS = &PerFunctionPassesConfigOS;
  94. PerFunctionPasses->add(
  95. createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
  96. }
  97. return PerFunctionPasses;
  98. }
  99. void CreatePasses();
  100. /// Generates the TargetMachine.
  101. /// Returns Null if it is unable to create the target machine.
  102. /// Some of our clang tests specify triples which are not built
  103. /// into clang. This is okay because these tests check the generated
  104. /// IR, and they require DataLayout which depends on the triple.
  105. /// In this case, we allow this method to fail and not report an error.
  106. /// When MustCreateTM is used, we print an error if we are unable to load
  107. /// the requested target.
  108. TargetMachine *CreateTargetMachine(bool MustCreateTM);
  109. /// Add passes necessary to emit assembly or LLVM IR.
  110. ///
  111. /// \return True on success.
  112. bool AddEmitPasses(BackendAction Action, raw_pwrite_stream &OS);
  113. public:
  114. EmitAssemblyHelper(DiagnosticsEngine &_Diags,
  115. const CodeGenOptions &CGOpts,
  116. const clang::TargetOptions &TOpts,
  117. const LangOptions &LOpts,
  118. Module *M)
  119. : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
  120. TheModule(M), CodeGenerationTime("Code Generation Time"),
  121. CodeGenPasses(nullptr), PerModulePasses(nullptr),
  122. PerFunctionPasses(nullptr),
  123. // HLSL Changes Start
  124. CodeGenPassesConfigOS(CodeGenPassesConfig),
  125. PerModulePassesConfigOS(PerModulePassesConfig),
  126. PerFunctionPassesConfigOS(PerFunctionPassesConfig) {}
  127. // HLSL Changes End
  128. ~EmitAssemblyHelper() {
  129. delete CodeGenPasses;
  130. delete PerModulePasses;
  131. delete PerFunctionPasses;
  132. if (CodeGenOpts.DisableFree)
  133. BuryPointer(std::move(TM));
  134. }
  135. std::unique_ptr<TargetMachine> TM;
  136. void EmitAssembly(BackendAction Action, raw_pwrite_stream *OS);
  137. };
  138. // We need this wrapper to access LangOpts and CGOpts from extension functions
  139. // that we add to the PassManagerBuilder.
  140. class PassManagerBuilderWrapper : public PassManagerBuilder {
  141. public:
  142. PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
  143. const LangOptions &LangOpts)
  144. : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
  145. const CodeGenOptions &getCGOpts() const { return CGOpts; }
  146. const LangOptions &getLangOpts() const { return LangOpts; }
  147. private:
  148. const CodeGenOptions &CGOpts;
  149. const LangOptions &LangOpts;
  150. };
  151. }
  152. #ifdef MS_ENABLE_OBJCARC // HLSL Change
  153. static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
  154. if (Builder.OptLevel > 0)
  155. PM.add(createObjCARCAPElimPass());
  156. }
  157. static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
  158. if (Builder.OptLevel > 0)
  159. PM.add(createObjCARCExpandPass());
  160. }
  161. static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
  162. if (Builder.OptLevel > 0)
  163. PM.add(createObjCARCOptPass());
  164. }
  165. #endif // MS_ENABLE_OBJCARC - HLSL Change
  166. static void addSampleProfileLoaderPass(const PassManagerBuilder &Builder,
  167. legacy::PassManagerBase &PM) {
  168. const PassManagerBuilderWrapper &BuilderWrapper =
  169. static_cast<const PassManagerBuilderWrapper &>(Builder);
  170. const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
  171. PM.add(createSampleProfileLoaderPass(CGOpts.SampleProfileFile));
  172. }
  173. static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
  174. legacy::PassManagerBase &PM) {
  175. PM.add(createAddDiscriminatorsPass());
  176. }
  177. static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
  178. legacy::PassManagerBase &PM) {
  179. PM.add(createBoundsCheckingPass());
  180. }
  181. #ifdef MS_ENABLE_INSTR // HLSL Change
  182. static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
  183. legacy::PassManagerBase &PM) {
  184. const PassManagerBuilderWrapper &BuilderWrapper =
  185. static_cast<const PassManagerBuilderWrapper&>(Builder);
  186. const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
  187. SanitizerCoverageOptions Opts;
  188. Opts.CoverageType =
  189. static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
  190. Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
  191. Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
  192. Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
  193. Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
  194. PM.add(createSanitizerCoverageModulePass(Opts));
  195. }
  196. static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
  197. legacy::PassManagerBase &PM) {
  198. PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/false));
  199. PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false));
  200. }
  201. static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
  202. legacy::PassManagerBase &PM) {
  203. PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/true));
  204. PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true));
  205. }
  206. static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
  207. legacy::PassManagerBase &PM) {
  208. const PassManagerBuilderWrapper &BuilderWrapper =
  209. static_cast<const PassManagerBuilderWrapper&>(Builder);
  210. const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
  211. PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins));
  212. // MemorySanitizer inserts complex instrumentation that mostly follows
  213. // the logic of the original code, but operates on "shadow" values.
  214. // It can benefit from re-running some general purpose optimization passes.
  215. if (Builder.OptLevel > 0) {
  216. PM.add(createEarlyCSEPass());
  217. PM.add(createReassociatePass());
  218. PM.add(createLICMPass());
  219. PM.add(createGVNPass());
  220. PM.add(createInstructionCombiningPass());
  221. PM.add(createDeadStoreEliminationPass());
  222. }
  223. }
  224. static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
  225. legacy::PassManagerBase &PM) {
  226. PM.add(createThreadSanitizerPass());
  227. }
  228. static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
  229. legacy::PassManagerBase &PM) {
  230. const PassManagerBuilderWrapper &BuilderWrapper =
  231. static_cast<const PassManagerBuilderWrapper&>(Builder);
  232. const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
  233. PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
  234. }
  235. #endif // MS_ENABLE_INSTR - HLSL Change
  236. static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
  237. const CodeGenOptions &CodeGenOpts) {
  238. TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
  239. if (!CodeGenOpts.SimplifyLibCalls)
  240. TLII->disableAllFunctions();
  241. switch (CodeGenOpts.getVecLib()) {
  242. case CodeGenOptions::Accelerate:
  243. TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
  244. break;
  245. default:
  246. break;
  247. }
  248. return TLII;
  249. }
  250. static void addSymbolRewriterPass(const CodeGenOptions &Opts,
  251. legacy::PassManager *MPM) {
  252. llvm::SymbolRewriter::RewriteDescriptorList DL;
  253. llvm::SymbolRewriter::RewriteMapParser MapParser;
  254. for (const auto &MapFile : Opts.RewriteMapFiles)
  255. MapParser.parse(MapFile, &DL);
  256. MPM->add(createRewriteSymbolsPass(DL));
  257. }
  258. void EmitAssemblyHelper::CreatePasses() {
  259. unsigned OptLevel = CodeGenOpts.OptimizationLevel;
  260. CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
  261. // Handle disabling of LLVM optimization, where we want to preserve the
  262. // internal module before any optimization.
  263. if (CodeGenOpts.DisableLLVMOpts || CodeGenOpts.HLSLHighLevel) { // HLSL Change
  264. OptLevel = 0;
  265. // HLSL Change Begins.
  266. // HLSL always inline.
  267. if (!LangOpts.HLSL || CodeGenOpts.HLSLHighLevel)
  268. Inlining = CodeGenOpts.NoInlining;
  269. // HLSL Change Ends.
  270. }
  271. PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
  272. PMBuilder.OptLevel = OptLevel;
  273. PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
  274. PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
  275. PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
  276. PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
  277. PMBuilder.HLSLHighLevel = CodeGenOpts.HLSLHighLevel; // HLSL Change
  278. PMBuilder.HLSLExtensionsCodeGen = CodeGenOpts.HLSLExtensionsCodegen.get(); // HLSL Change
  279. PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
  280. PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
  281. PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
  282. PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
  283. PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
  284. PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
  285. addAddDiscriminatorsPass);
  286. if (!CodeGenOpts.SampleProfileFile.empty())
  287. PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
  288. addSampleProfileLoaderPass);
  289. // In ObjC ARC mode, add the main ARC optimization passes.
  290. #ifdef MS_ENABLE_OBJCARC // HLSL Change
  291. if (LangOpts.ObjCAutoRefCount) {
  292. PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
  293. addObjCARCExpandPass);
  294. PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
  295. addObjCARCAPElimPass);
  296. PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
  297. addObjCARCOptPass);
  298. }
  299. if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
  300. PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
  301. addBoundsCheckingPass);
  302. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  303. addBoundsCheckingPass);
  304. }
  305. #endif // MS_ENABLE_OBJCARC - HLSL Change
  306. #ifdef MS_ENABLE_INSTR // HLSL Change
  307. if (CodeGenOpts.SanitizeCoverageType ||
  308. CodeGenOpts.SanitizeCoverageIndirectCalls ||
  309. CodeGenOpts.SanitizeCoverageTraceCmp) {
  310. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  311. addSanitizerCoveragePass);
  312. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  313. addSanitizerCoveragePass);
  314. }
  315. if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
  316. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  317. addAddressSanitizerPasses);
  318. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  319. addAddressSanitizerPasses);
  320. }
  321. if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
  322. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  323. addKernelAddressSanitizerPasses);
  324. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  325. addKernelAddressSanitizerPasses);
  326. }
  327. if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
  328. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  329. addMemorySanitizerPass);
  330. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  331. addMemorySanitizerPass);
  332. }
  333. if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
  334. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  335. addThreadSanitizerPass);
  336. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  337. addThreadSanitizerPass);
  338. }
  339. if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
  340. PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
  341. addDataFlowSanitizerPass);
  342. PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
  343. addDataFlowSanitizerPass);
  344. }
  345. #endif // MS_ENABLE_INSTR - HLSL Change
  346. // Figure out TargetLibraryInfo.
  347. Triple TargetTriple(TheModule->getTargetTriple());
  348. PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
  349. switch (Inlining) {
  350. case CodeGenOptions::NoInlining: break;
  351. case CodeGenOptions::NormalInlining: {
  352. PMBuilder.Inliner =
  353. createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
  354. break;
  355. }
  356. case CodeGenOptions::OnlyAlwaysInlining:
  357. // Respect always_inline.
  358. if (OptLevel == 0)
  359. // Do not insert lifetime intrinsics at -O0.
  360. PMBuilder.Inliner = createAlwaysInlinerPass(false);
  361. else
  362. PMBuilder.Inliner = createAlwaysInlinerPass();
  363. break;
  364. }
  365. // Set up the per-function pass manager.
  366. legacy::FunctionPassManager *FPM = getPerFunctionPasses();
  367. if (CodeGenOpts.VerifyModule)
  368. FPM->add(createVerifierPass());
  369. PMBuilder.populateFunctionPassManager(*FPM);
  370. // Set up the per-module pass manager.
  371. legacy::PassManager *MPM = getPerModulePasses();
  372. if (!CodeGenOpts.RewriteMapFiles.empty())
  373. addSymbolRewriterPass(CodeGenOpts, MPM);
  374. #ifdef MS_ENABLE_INSTR // HLSL Change
  375. if (!CodeGenOpts.DisableGCov &&
  376. (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
  377. // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
  378. // LLVM's -default-gcov-version flag is set to something invalid.
  379. GCOVOptions Options;
  380. Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
  381. Options.EmitData = CodeGenOpts.EmitGcovArcs;
  382. memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
  383. Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
  384. Options.NoRedZone = CodeGenOpts.DisableRedZone;
  385. Options.FunctionNamesInData =
  386. !CodeGenOpts.CoverageNoFunctionNamesInData;
  387. Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
  388. MPM->add(createGCOVProfilerPass(Options));
  389. if (CodeGenOpts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
  390. MPM->add(createStripSymbolsPass(true));
  391. }
  392. if (CodeGenOpts.ProfileInstrGenerate) {
  393. InstrProfOptions Options;
  394. Options.NoRedZone = CodeGenOpts.DisableRedZone;
  395. Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
  396. MPM->add(createInstrProfilingPass(Options));
  397. }
  398. #endif
  399. PMBuilder.populateModulePassManager(*MPM);
  400. }
  401. TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
  402. // Create the TargetMachine for generating code.
  403. std::string Error;
  404. std::string Triple = TheModule->getTargetTriple();
  405. const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
  406. if (!TheTarget) {
  407. if (MustCreateTM)
  408. Diags.Report(diag::err_fe_unable_to_create_target) << Error;
  409. return nullptr;
  410. }
  411. unsigned CodeModel =
  412. llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
  413. .Case("small", llvm::CodeModel::Small)
  414. .Case("kernel", llvm::CodeModel::Kernel)
  415. .Case("medium", llvm::CodeModel::Medium)
  416. .Case("large", llvm::CodeModel::Large)
  417. .Case("default", llvm::CodeModel::Default)
  418. .Default(~0u);
  419. assert(CodeModel != ~0u && "invalid code model!");
  420. llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
  421. #if 0 // HLSL Change - no global state mutation on per-instance work
  422. SmallVector<const char *, 16> BackendArgs;
  423. BackendArgs.push_back("clang"); // Fake program name.
  424. if (!CodeGenOpts.DebugPass.empty()) {
  425. BackendArgs.push_back("-debug-pass");
  426. BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
  427. }
  428. if (!CodeGenOpts.LimitFloatPrecision.empty()) {
  429. BackendArgs.push_back("-limit-float-precision");
  430. BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
  431. }
  432. for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i)
  433. BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str());
  434. BackendArgs.push_back(nullptr);
  435. llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
  436. BackendArgs.data());
  437. #endif // HLSL Change
  438. std::string FeaturesStr;
  439. if (!TargetOpts.Features.empty()) {
  440. #if 0 // HLSL Change
  441. SubtargetFeatures Features;
  442. for (const std::string &Feature : TargetOpts.Features)
  443. Features.AddFeature(Feature);
  444. FeaturesStr = Features.getString();
  445. #endif // HLSL Change
  446. }
  447. llvm::Reloc::Model RM = llvm::Reloc::Default;
  448. if (CodeGenOpts.RelocationModel == "static") {
  449. RM = llvm::Reloc::Static;
  450. } else if (CodeGenOpts.RelocationModel == "pic") {
  451. RM = llvm::Reloc::PIC_;
  452. } else {
  453. assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
  454. "Invalid PIC model!");
  455. RM = llvm::Reloc::DynamicNoPIC;
  456. }
  457. CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
  458. switch (CodeGenOpts.OptimizationLevel) {
  459. default: break;
  460. case 0: OptLevel = CodeGenOpt::None; break;
  461. case 3: OptLevel = CodeGenOpt::Aggressive; break;
  462. }
  463. llvm::TargetOptions Options;
  464. if (!TargetOpts.Reciprocals.empty())
  465. Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals);
  466. Options.ThreadModel =
  467. llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
  468. .Case("posix", llvm::ThreadModel::POSIX)
  469. .Case("single", llvm::ThreadModel::Single);
  470. if (CodeGenOpts.DisableIntegratedAS)
  471. Options.DisableIntegratedAS = true;
  472. if (CodeGenOpts.CompressDebugSections)
  473. Options.CompressDebugSections = true;
  474. if (CodeGenOpts.UseInitArray)
  475. Options.UseInitArray = true;
  476. // Set float ABI type.
  477. if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp")
  478. Options.FloatABIType = llvm::FloatABI::Soft;
  479. else if (CodeGenOpts.FloatABI == "hard")
  480. Options.FloatABIType = llvm::FloatABI::Hard;
  481. else {
  482. assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
  483. Options.FloatABIType = llvm::FloatABI::Default;
  484. }
  485. // Set FP fusion mode.
  486. switch (CodeGenOpts.getFPContractMode()) {
  487. case CodeGenOptions::FPC_Off:
  488. Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
  489. break;
  490. case CodeGenOptions::FPC_On:
  491. Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
  492. break;
  493. case CodeGenOptions::FPC_Fast:
  494. Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
  495. break;
  496. }
  497. Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
  498. Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
  499. Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
  500. Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
  501. Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
  502. Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
  503. Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
  504. Options.FunctionSections = CodeGenOpts.FunctionSections;
  505. Options.DataSections = CodeGenOpts.DataSections;
  506. Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
  507. Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
  508. Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
  509. Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
  510. Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
  511. Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
  512. Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
  513. Options.MCOptions.ABIName = TargetOpts.ABI;
  514. TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
  515. FeaturesStr, Options,
  516. RM, CM, OptLevel);
  517. return TM;
  518. }
  519. bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
  520. raw_pwrite_stream &OS) {
  521. // Create the code generator passes.
  522. legacy::PassManager *PM = getCodeGenPasses();
  523. // Add LibraryInfo.
  524. llvm::Triple TargetTriple(TheModule->getTargetTriple());
  525. std::unique_ptr<TargetLibraryInfoImpl> TLII(
  526. createTLII(TargetTriple, CodeGenOpts));
  527. PM->add(new TargetLibraryInfoWrapperPass(*TLII));
  528. // Normal mode, emit a .s or .o file by running the code generator. Note,
  529. // this also adds codegenerator level optimization passes.
  530. TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
  531. if (Action == Backend_EmitObj)
  532. CGFT = TargetMachine::CGFT_ObjectFile;
  533. else if (Action == Backend_EmitMCNull)
  534. CGFT = TargetMachine::CGFT_Null;
  535. else
  536. assert(Action == Backend_EmitAssembly && "Invalid action!");
  537. // Add ObjC ARC final-cleanup optimizations. This is done as part of the
  538. // "codegen" passes so that it isn't run multiple times when there is
  539. // inlining happening.
  540. #ifdef MS_ENABLE_OBJCARC // HLSL Change
  541. if (CodeGenOpts.OptimizationLevel > 0)
  542. PM->add(createObjCARCContractPass());
  543. #endif // HLSL Change
  544. if (TM->addPassesToEmitFile(*PM, OS, CGFT,
  545. /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
  546. Diags.Report(diag::err_fe_unable_to_interface_with_target);
  547. return false;
  548. }
  549. return true;
  550. }
  551. void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
  552. raw_pwrite_stream *OS) {
  553. TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
  554. bool UsesCodeGen = (Action != Backend_EmitNothing &&
  555. Action != Backend_EmitBC &&
  556. Action != Backend_EmitPasses &&
  557. Action != Backend_EmitLL);
  558. if (!TM)
  559. TM.reset(CreateTargetMachine(UsesCodeGen));
  560. if (UsesCodeGen && !TM)
  561. return;
  562. if (TM)
  563. TheModule->setDataLayout(*TM->getDataLayout());
  564. CreatePasses();
  565. switch (Action) {
  566. case Backend_EmitNothing:
  567. case Backend_EmitPasses: // HLSL Change
  568. break;
  569. case Backend_EmitBC:
  570. getPerModulePasses()->add(
  571. createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
  572. break;
  573. case Backend_EmitLL:
  574. getPerModulePasses()->add(
  575. createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
  576. break;
  577. default:
  578. if (!AddEmitPasses(Action, *OS))
  579. return;
  580. }
  581. // Before executing passes, print the final values of the LLVM options.
  582. cl::PrintOptionValues();
  583. // HLSL Change Starts
  584. if (Action == Backend_EmitPasses) {
  585. if (PerFunctionPasses) {
  586. *OS << "# Per-function passes\n"
  587. "-opt-fn-passes\n";
  588. *OS << PerFunctionPassesConfigOS.str();
  589. }
  590. if (PerModulePasses) {
  591. *OS << "# Per-module passes\n"
  592. "-opt-mod-passes\n";
  593. *OS << PerModulePassesConfigOS.str();
  594. }
  595. if (CodeGenPasses) {
  596. *OS << "# Code generation passes\n"
  597. "-opt-mod-passes\n";
  598. *OS << CodeGenPassesConfigOS.str();
  599. }
  600. return;
  601. }
  602. // HLSL Change Ends
  603. // Run passes. For now we do all passes at once, but eventually we
  604. // would like to have the option of streaming code generation.
  605. if (PerFunctionPasses) {
  606. PrettyStackTraceString CrashInfo("Per-function optimization");
  607. PerFunctionPasses->doInitialization();
  608. for (Function &F : *TheModule)
  609. if (!F.isDeclaration())
  610. PerFunctionPasses->run(F);
  611. PerFunctionPasses->doFinalization();
  612. }
  613. if (PerModulePasses) {
  614. PrettyStackTraceString CrashInfo("Per-module optimization passes");
  615. PerModulePasses->run(*TheModule);
  616. }
  617. if (CodeGenPasses) {
  618. PrettyStackTraceString CrashInfo("Code generation");
  619. CodeGenPasses->run(*TheModule);
  620. }
  621. }
  622. void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
  623. const CodeGenOptions &CGOpts,
  624. const clang::TargetOptions &TOpts,
  625. const LangOptions &LOpts, StringRef TDesc,
  626. Module *M, BackendAction Action,
  627. raw_pwrite_stream *OS) {
  628. EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
  629. AsmHelper.EmitAssembly(Action, OS);
  630. // If an optional clang TargetInfo description string was passed in, use it to
  631. // verify the LLVM TargetMachine's DataLayout.
  632. if (AsmHelper.TM && !TDesc.empty()) {
  633. std::string DLDesc =
  634. AsmHelper.TM->getDataLayout()->getStringRepresentation();
  635. if (DLDesc != TDesc) {
  636. unsigned DiagID = Diags.getCustomDiagID(
  637. DiagnosticsEngine::Error, "backend data layout '%0' does not match "
  638. "expected target description '%1'");
  639. Diags.Report(DiagID) << DLDesc << TDesc;
  640. }
  641. }
  642. }