BackendUtil.cpp 28 KB

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