Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 5e3b677 | 2012-03-21 08:02:10 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_backend.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/Decl.h" |
| 24 | #include "clang/AST/DeclGroup.h" |
| 25 | |
| 26 | #include "clang/Basic/Diagnostic.h" |
| 27 | #include "clang/Basic/TargetInfo.h" |
| 28 | #include "clang/Basic/TargetOptions.h" |
| 29 | |
| 30 | #include "clang/CodeGen/ModuleBuilder.h" |
| 31 | |
| 32 | #include "clang/Frontend/CodeGenOptions.h" |
| 33 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 34 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 35 | #include "llvm/ADT/Twine.h" |
| 36 | #include "llvm/ADT/StringExtras.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 37 | |
| 38 | #include "llvm/Bitcode/ReaderWriter.h" |
| 39 | |
| 40 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 41 | #include "llvm/CodeGen/SchedulerRegistry.h" |
| 42 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 43 | #include "llvm/IR/Constant.h" |
| 44 | #include "llvm/IR/Constants.h" |
| 45 | #include "llvm/IR/DataLayout.h" |
| 46 | #include "llvm/IR/DebugLoc.h" |
| 47 | #include "llvm/IR/DerivedTypes.h" |
| 48 | #include "llvm/IR/Function.h" |
| 49 | #include "llvm/IR/IRBuilder.h" |
| 50 | #include "llvm/IR/IRPrintingPasses.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 51 | #include "llvm/IR/LLVMContext.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 52 | #include "llvm/IR/Metadata.h" |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 53 | #include "llvm/IR/Module.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 54 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 55 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 56 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 57 | #include "llvm/Target/TargetMachine.h" |
| 58 | #include "llvm/Target/TargetOptions.h" |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 59 | #include "llvm/Support/TargetRegistry.h" |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 60 | |
| 61 | #include "llvm/MC/SubtargetFeature.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 62 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 63 | #include "slang_assert.h" |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 64 | #include "slang.h" |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 65 | #include "slang_bitcode_gen.h" |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 66 | #include "slang_rs_context.h" |
| 67 | #include "slang_rs_export_foreach.h" |
| 68 | #include "slang_rs_export_func.h" |
Matt Wala | c0c5dd8 | 2015-07-23 17:29:37 -0700 | [diff] [blame] | 69 | #include "slang_rs_export_reduce.h" |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 70 | #include "slang_rs_export_type.h" |
| 71 | #include "slang_rs_export_var.h" |
| 72 | #include "slang_rs_metadata.h" |
| 73 | |
David Gross | 2770d0e | 2015-08-03 14:58:59 -0700 | [diff] [blame] | 74 | #include "rs_cc_options.h" |
| 75 | |
Stephen Hines | 552d872 | 2013-12-18 12:21:50 -0800 | [diff] [blame] | 76 | #include "strip_unknown_attributes.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 77 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 78 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 79 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 80 | void Backend::CreateFunctionPasses() { |
| 81 | if (!mPerFunctionPasses) { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 82 | mPerFunctionPasses = new llvm::legacy::FunctionPassManager(mpModule); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 83 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 84 | llvm::PassManagerBuilder PMBuilder; |
| 85 | PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel; |
| 86 | PMBuilder.populateFunctionPassManager(*mPerFunctionPasses); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 87 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void Backend::CreateModulePasses() { |
| 91 | if (!mPerModulePasses) { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 92 | mPerModulePasses = new llvm::legacy::PassManager(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 93 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 94 | llvm::PassManagerBuilder PMBuilder; |
| 95 | PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel; |
| 96 | PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize; |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 97 | if (mCodeGenOpts.UnitAtATime) { |
| 98 | PMBuilder.DisableUnitAtATime = 0; |
| 99 | } else { |
| 100 | PMBuilder.DisableUnitAtATime = 1; |
| 101 | } |
| 102 | |
| 103 | if (mCodeGenOpts.UnrollLoops) { |
| 104 | PMBuilder.DisableUnrollLoops = 0; |
| 105 | } else { |
| 106 | PMBuilder.DisableUnrollLoops = 1; |
| 107 | } |
| 108 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 109 | PMBuilder.populateModulePassManager(*mPerModulePasses); |
Stephen Hines | 552d872 | 2013-12-18 12:21:50 -0800 | [diff] [blame] | 110 | // Add a pass to strip off unknown/unsupported attributes. |
| 111 | mPerModulePasses->add(createStripUnknownAttributesPass()); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 112 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | bool Backend::CreateCodeGenPasses() { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 116 | if ((mOT != Slang::OT_Assembly) && (mOT != Slang::OT_Object)) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 117 | return true; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 118 | |
| 119 | // Now we add passes for code emitting |
| 120 | if (mCodeGenPasses) { |
| 121 | return true; |
| 122 | } else { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 123 | mCodeGenPasses = new llvm::legacy::FunctionPassManager(mpModule); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Create the TargetMachine for generating code. |
| 127 | std::string Triple = mpModule->getTargetTriple(); |
| 128 | |
| 129 | std::string Error; |
| 130 | const llvm::Target* TargetInfo = |
| 131 | llvm::TargetRegistry::lookupTarget(Triple, Error); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 132 | if (TargetInfo == nullptr) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 133 | mDiagEngine.Report(clang::diag::err_fe_unable_to_create_target) << Error; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Logan Chien | ac4e185 | 2011-12-16 13:37:10 +0800 | [diff] [blame] | 137 | // Target Machine Options |
| 138 | llvm::TargetOptions Options; |
| 139 | |
Pirama Arumuga Nainar | 167bd79 | 2015-06-30 19:46:13 -0700 | [diff] [blame] | 140 | // Use soft-float ABI for ARM (which is the target used by Slang during code |
| 141 | // generation). Codegen still uses hardware FPU by default. To use software |
| 142 | // floating point, add 'soft-float' feature to FeaturesStr below. |
| 143 | Options.FloatABIType = llvm::FloatABI::Soft; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 144 | |
| 145 | // BCC needs all unknown symbols resolved at compilation time. So we don't |
| 146 | // need any relocation model. |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 147 | llvm::Reloc::Model RM = llvm::Reloc::Static; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 148 | |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 149 | // This is set for the linker (specify how large of the virtual addresses we |
| 150 | // can access for all unknown symbols.) |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 151 | llvm::CodeModel::Model CM; |
Stephen Hines | 0b75458 | 2015-04-07 13:59:57 -0700 | [diff] [blame] | 152 | if (mpModule->getDataLayout().getPointerSize() == 4) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 153 | CM = llvm::CodeModel::Small; |
| 154 | } else { |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 155 | // The target may have pointer size greater than 32 (e.g. x86_64 |
| 156 | // architecture) may need large data address model |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 157 | CM = llvm::CodeModel::Medium; |
| 158 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 159 | |
| 160 | // Setup feature string |
| 161 | std::string FeaturesStr; |
| 162 | if (mTargetOpts.CPU.size() || mTargetOpts.Features.size()) { |
| 163 | llvm::SubtargetFeatures Features; |
| 164 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 165 | for (std::vector<std::string>::const_iterator |
| 166 | I = mTargetOpts.Features.begin(), E = mTargetOpts.Features.end(); |
| 167 | I != E; |
| 168 | I++) |
| 169 | Features.AddFeature(*I); |
| 170 | |
| 171 | FeaturesStr = Features.getString(); |
| 172 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 173 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 174 | llvm::TargetMachine *TM = |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 175 | TargetInfo->createTargetMachine(Triple, mTargetOpts.CPU, FeaturesStr, |
Logan Chien | ac4e185 | 2011-12-16 13:37:10 +0800 | [diff] [blame] | 176 | Options, RM, CM); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 177 | |
| 178 | // Register scheduler |
| 179 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 180 | |
| 181 | // Register allocation policy: |
| 182 | // createFastRegisterAllocator: fast but bad quality |
Logan Chien | 2c6bad5 | 2011-11-15 15:57:33 +0800 | [diff] [blame] | 183 | // createGreedyRegisterAllocator: not so fast but good quality |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 184 | llvm::RegisterRegAlloc::setDefault((mCodeGenOpts.OptimizationLevel == 0) ? |
| 185 | llvm::createFastRegisterAllocator : |
Logan Chien | 2c6bad5 | 2011-11-15 15:57:33 +0800 | [diff] [blame] | 186 | llvm::createGreedyRegisterAllocator); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 187 | |
| 188 | llvm::CodeGenOpt::Level OptLevel = llvm::CodeGenOpt::Default; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 189 | if (mCodeGenOpts.OptimizationLevel == 0) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 190 | OptLevel = llvm::CodeGenOpt::None; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 191 | } else if (mCodeGenOpts.OptimizationLevel == 3) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 192 | OptLevel = llvm::CodeGenOpt::Aggressive; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 193 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 194 | |
| 195 | llvm::TargetMachine::CodeGenFileType CGFT = |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 196 | llvm::TargetMachine::CGFT_AssemblyFile; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 197 | if (mOT == Slang::OT_Object) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 198 | CGFT = llvm::TargetMachine::CGFT_ObjectFile; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 199 | } |
Pirama Arumuga Nainar | 21cc018 | 2015-05-06 11:17:16 -0700 | [diff] [blame] | 200 | if (TM->addPassesToEmitFile(*mCodeGenPasses, mBufferOutStream, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 201 | CGFT, OptLevel)) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 202 | mDiagEngine.Report(clang::diag::err_fe_unable_to_interface_with_target); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 209 | Backend::Backend(RSContext *Context, clang::DiagnosticsEngine *DiagEngine, |
David Gross | 2770d0e | 2015-08-03 14:58:59 -0700 | [diff] [blame] | 210 | const RSCCOptions &Opts, const clang::CodeGenOptions &CodeGenOpts, |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 211 | const clang::TargetOptions &TargetOpts, PragmaList *Pragmas, |
| 212 | llvm::raw_ostream *OS, Slang::OutputType OT, |
| 213 | clang::SourceManager &SourceMgr, bool AllowRSPrefix, |
| 214 | bool IsFilterscript) |
| 215 | : ASTConsumer(), mTargetOpts(TargetOpts), mpModule(nullptr), mpOS(OS), |
| 216 | mOT(OT), mGen(nullptr), mPerFunctionPasses(nullptr), |
Pirama Arumuga Nainar | 21cc018 | 2015-05-06 11:17:16 -0700 | [diff] [blame] | 217 | mPerModulePasses(nullptr), mCodeGenPasses(nullptr), |
| 218 | mBufferOutStream(*mpOS), mContext(Context), |
David Gross | 2770d0e | 2015-08-03 14:58:59 -0700 | [diff] [blame] | 219 | mSourceMgr(SourceMgr), mASTPrint(Opts.mASTPrint), mAllowRSPrefix(AllowRSPrefix), |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 220 | mIsFilterscript(IsFilterscript), mExportVarMetadata(nullptr), |
| 221 | mExportFuncMetadata(nullptr), mExportForEachNameMetadata(nullptr), |
Matt Wala | c0c5dd8 | 2015-07-23 17:29:37 -0700 | [diff] [blame] | 222 | mExportForEachSignatureMetadata(nullptr), mExportReduceMetadata(nullptr), |
| 223 | mExportTypeMetadata(nullptr), mRSObjectSlotsMetadata(nullptr), |
| 224 | mRefCount(mContext->getASTContext()), |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 225 | mASTChecker(Context, Context->getTargetAPI(), IsFilterscript), |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 226 | mForEachHandler(Context), |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 227 | mLLVMContext(llvm::getGlobalContext()), mDiagEngine(*DiagEngine), |
| 228 | mCodeGenOpts(CodeGenOpts), mPragmas(Pragmas) { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 229 | mGen = CreateLLVMCodeGen(mDiagEngine, "", mCodeGenOpts, mLLVMContext); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 232 | void Backend::Initialize(clang::ASTContext &Ctx) { |
| 233 | mGen->Initialize(Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 234 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 235 | mpModule = mGen->GetModule(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 238 | void Backend::HandleTranslationUnit(clang::ASTContext &Ctx) { |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 239 | HandleTranslationUnitPre(Ctx); |
| 240 | |
David Gross | 2770d0e | 2015-08-03 14:58:59 -0700 | [diff] [blame] | 241 | if (mASTPrint) |
| 242 | Ctx.getTranslationUnitDecl()->dump(); |
| 243 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 244 | mGen->HandleTranslationUnit(Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 245 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 246 | // Here, we complete a translation unit (whole translation unit is now in LLVM |
| 247 | // IR). Now, interact with LLVM backend to generate actual machine code (asm |
| 248 | // or machine code, whatever.) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 249 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 250 | // Silently ignore if we weren't initialized for some reason. |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 251 | if (!mpModule) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 252 | return; |
| 253 | |
| 254 | llvm::Module *M = mGen->ReleaseModule(); |
| 255 | if (!M) { |
| 256 | // The module has been released by IR gen on failures, do not double free. |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 257 | mpModule = nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 261 | slangAssert(mpModule == M && |
| 262 | "Unexpected module change during LLVM IR generation"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 263 | |
| 264 | // Insert #pragma information into metadata section of module |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 265 | if (!mPragmas->empty()) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 266 | llvm::NamedMDNode *PragmaMetadata = |
| 267 | mpModule->getOrInsertNamedMetadata(Slang::PragmaMetadataName); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 268 | for (PragmaList::const_iterator I = mPragmas->begin(), E = mPragmas->end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 269 | I != E; |
| 270 | I++) { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 271 | llvm::SmallVector<llvm::Metadata*, 2> Pragma; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 272 | // Name goes first |
| 273 | Pragma.push_back(llvm::MDString::get(mLLVMContext, I->first)); |
| 274 | // And then value |
| 275 | Pragma.push_back(llvm::MDString::get(mLLVMContext, I->second)); |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 276 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 277 | // Create MDNode and insert into PragmaMetadata |
| 278 | PragmaMetadata->addOperand( |
Stephen Hines | 18c8829 | 2011-07-14 22:46:15 -0700 | [diff] [blame] | 279 | llvm::MDNode::get(mLLVMContext, Pragma)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 283 | HandleTranslationUnitPost(mpModule); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 284 | |
| 285 | // Create passes for optimization and code emission |
| 286 | |
| 287 | // Create and run per-function passes |
| 288 | CreateFunctionPasses(); |
| 289 | if (mPerFunctionPasses) { |
| 290 | mPerFunctionPasses->doInitialization(); |
| 291 | |
| 292 | for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end(); |
| 293 | I != E; |
| 294 | I++) |
| 295 | if (!I->isDeclaration()) |
| 296 | mPerFunctionPasses->run(*I); |
| 297 | |
| 298 | mPerFunctionPasses->doFinalization(); |
| 299 | } |
| 300 | |
| 301 | // Create and run module passes |
| 302 | CreateModulePasses(); |
| 303 | if (mPerModulePasses) |
| 304 | mPerModulePasses->run(*mpModule); |
| 305 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 306 | switch (mOT) { |
| 307 | case Slang::OT_Assembly: |
| 308 | case Slang::OT_Object: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 309 | if (!CreateCodeGenPasses()) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 310 | return; |
| 311 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 312 | mCodeGenPasses->doInitialization(); |
| 313 | |
| 314 | for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end(); |
| 315 | I != E; |
| 316 | I++) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 317 | if (!I->isDeclaration()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 318 | mCodeGenPasses->run(*I); |
| 319 | |
| 320 | mCodeGenPasses->doFinalization(); |
| 321 | break; |
| 322 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 323 | case Slang::OT_LLVMAssembly: { |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 324 | llvm::legacy::PassManager *LLEmitPM = new llvm::legacy::PassManager(); |
Pirama Arumuga Nainar | 21cc018 | 2015-05-06 11:17:16 -0700 | [diff] [blame] | 325 | LLEmitPM->add(llvm::createPrintModulePass(mBufferOutStream)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 326 | LLEmitPM->run(*mpModule); |
| 327 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 328 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 329 | case Slang::OT_Bitcode: { |
Matt Wala | dabd246 | 2015-07-15 18:39:41 -0700 | [diff] [blame] | 330 | writeBitcode(mBufferOutStream, *mpModule, getTargetAPI(), |
Stephen McGroarty | b130e15 | 2015-07-23 16:55:02 +0100 | [diff] [blame] | 331 | mCodeGenOpts.OptimizationLevel, mCodeGenOpts.getDebugInfo()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 332 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 333 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 334 | case Slang::OT_Nothing: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 335 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 336 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 337 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 338 | slangAssert(false && "Unknown output type"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 339 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 340 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 341 | |
Pirama Arumuga Nainar | 21cc018 | 2015-05-06 11:17:16 -0700 | [diff] [blame] | 342 | mBufferOutStream.flush(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 345 | void Backend::HandleTagDeclDefinition(clang::TagDecl *D) { |
| 346 | mGen->HandleTagDeclDefinition(D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 349 | void Backend::CompleteTentativeDefinition(clang::VarDecl *D) { |
| 350 | mGen->CompleteTentativeDefinition(D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | Backend::~Backend() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 354 | delete mpModule; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 355 | delete mGen; |
| 356 | delete mPerFunctionPasses; |
| 357 | delete mPerModulePasses; |
| 358 | delete mCodeGenPasses; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 359 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 360 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 361 | // 1) Add zero initialization of local RS object types |
| 362 | void Backend::AnnotateFunction(clang::FunctionDecl *FD) { |
| 363 | if (FD && |
| 364 | FD->hasBody() && |
| 365 | !Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr)) { |
| 366 | mRefCount.Init(); |
| 367 | mRefCount.Visit(FD->getBody()); |
| 368 | } |
| 369 | } |
| 370 | |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 371 | void Backend::LowerRSForEachCall(clang::FunctionDecl *FD) { |
| 372 | // Skip this AST walking for lower API levels. |
| 373 | if (getTargetAPI() < SLANG_DEVELOPMENT_TARGET_API) { |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | if (!FD || !FD->hasBody() || |
| 378 | Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr)) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | mForEachHandler.VisitStmt(FD->getBody()); |
| 383 | } |
| 384 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 385 | bool Backend::HandleTopLevelDecl(clang::DeclGroupRef D) { |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 386 | // Find and remember the TypeDecl for rs_allocation so we can use it |
| 387 | // later during the compilation |
| 388 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); |
| 389 | I != E; I++) { |
| 390 | clang::TypeDecl* TD = llvm::dyn_cast<clang::TypeDecl>(*I); |
| 391 | if (TD && TD->getName().equals("rs_allocation")) { |
| 392 | mContext->addAllocationType(TD); |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 397 | // Disallow user-defined functions with prefix "rs" |
| 398 | if (!mAllowRSPrefix) { |
| 399 | // Iterate all function declarations in the program. |
| 400 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); |
| 401 | I != E; I++) { |
| 402 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
| 403 | if (FD == nullptr) |
| 404 | continue; |
| 405 | if (!FD->getName().startswith("rs")) // Check prefix |
| 406 | continue; |
| 407 | if (!Slang::IsLocInRSHeaderFile(FD->getLocation(), mSourceMgr)) |
| 408 | mContext->ReportError(FD->getLocation(), |
| 409 | "invalid function name prefix, " |
| 410 | "\"rs\" is reserved: '%0'") |
| 411 | << FD->getName(); |
| 412 | } |
| 413 | } |
| 414 | |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 415 | mContext->processExportDecl(D); |
| 416 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 417 | // Process any non-static function declarations |
| 418 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) { |
| 419 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
| 420 | if (FD && FD->isGlobal()) { |
| 421 | // Check that we don't have any array parameters being misintrepeted as |
| 422 | // kernel pointers due to the C type system's array to pointer decay. |
| 423 | size_t numParams = FD->getNumParams(); |
| 424 | for (size_t i = 0; i < numParams; i++) { |
| 425 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 426 | clang::QualType QT = PVD->getOriginalType(); |
| 427 | if (QT->isArrayType()) { |
| 428 | mContext->ReportError( |
| 429 | PVD->getTypeSpecStartLoc(), |
| 430 | "exported function parameters may not have array type: %0") |
| 431 | << QT; |
| 432 | } |
| 433 | } |
| 434 | AnnotateFunction(FD); |
| 435 | } |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 436 | |
| 437 | LowerRSForEachCall(FD); |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 438 | } |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 439 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 440 | return mGen->HandleTopLevelDecl(D); |
| 441 | } |
| 442 | |
| 443 | void Backend::HandleTranslationUnitPre(clang::ASTContext &C) { |
| 444 | clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 445 | |
| 446 | // If we have an invalid RS/FS AST, don't check further. |
| 447 | if (!mASTChecker.Validate()) { |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (mIsFilterscript) { |
| 452 | mContext->addPragma("rs_fp_relaxed", ""); |
| 453 | } |
| 454 | |
| 455 | int version = mContext->getVersion(); |
| 456 | if (version == 0) { |
| 457 | // Not setting a version is an error |
| 458 | mDiagEngine.Report( |
| 459 | mSourceMgr.getLocForEndOfFile(mSourceMgr.getMainFileID()), |
| 460 | mDiagEngine.getCustomDiagID( |
| 461 | clang::DiagnosticsEngine::Error, |
| 462 | "missing pragma for version in source file")); |
| 463 | } else { |
| 464 | slangAssert(version == 1); |
| 465 | } |
| 466 | |
| 467 | if (mContext->getReflectJavaPackageName().empty()) { |
| 468 | mDiagEngine.Report( |
| 469 | mSourceMgr.getLocForEndOfFile(mSourceMgr.getMainFileID()), |
| 470 | mDiagEngine.getCustomDiagID(clang::DiagnosticsEngine::Error, |
| 471 | "missing \"#pragma rs " |
| 472 | "java_package_name(com.foo.bar)\" " |
| 473 | "in source file")); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | // Create a static global destructor if necessary (to handle RS object |
| 478 | // runtime cleanup). |
| 479 | clang::FunctionDecl *FD = mRefCount.CreateStaticGlobalDtor(); |
| 480 | if (FD) { |
| 481 | HandleTopLevelDecl(clang::DeclGroupRef(FD)); |
| 482 | } |
| 483 | |
| 484 | // Process any static function declarations |
| 485 | for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(), |
| 486 | E = TUDecl->decls_end(); I != E; I++) { |
| 487 | if ((I->getKind() >= clang::Decl::firstFunction) && |
| 488 | (I->getKind() <= clang::Decl::lastFunction)) { |
| 489 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
| 490 | if (FD && !FD->isGlobal()) { |
| 491 | AnnotateFunction(FD); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /////////////////////////////////////////////////////////////////////////////// |
| 498 | void Backend::dumpExportVarInfo(llvm::Module *M) { |
| 499 | int slotCount = 0; |
| 500 | if (mExportVarMetadata == nullptr) |
| 501 | mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN); |
| 502 | |
| 503 | llvm::SmallVector<llvm::Metadata *, 2> ExportVarInfo; |
| 504 | |
| 505 | // We emit slot information (#rs_object_slots) for any reference counted |
| 506 | // RS type or pointer (which can also be bound). |
| 507 | |
| 508 | for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(), |
| 509 | E = mContext->export_vars_end(); |
| 510 | I != E; |
| 511 | I++) { |
| 512 | const RSExportVar *EV = *I; |
| 513 | const RSExportType *ET = EV->getType(); |
| 514 | bool countsAsRSObject = false; |
| 515 | |
| 516 | // Variable name |
| 517 | ExportVarInfo.push_back( |
| 518 | llvm::MDString::get(mLLVMContext, EV->getName().c_str())); |
| 519 | |
| 520 | // Type name |
| 521 | switch (ET->getClass()) { |
| 522 | case RSExportType::ExportClassPrimitive: { |
| 523 | const RSExportPrimitiveType *PT = |
| 524 | static_cast<const RSExportPrimitiveType*>(ET); |
| 525 | ExportVarInfo.push_back( |
| 526 | llvm::MDString::get( |
| 527 | mLLVMContext, llvm::utostr_32(PT->getType()))); |
| 528 | if (PT->isRSObjectType()) { |
| 529 | countsAsRSObject = true; |
| 530 | } |
| 531 | break; |
| 532 | } |
| 533 | case RSExportType::ExportClassPointer: { |
| 534 | ExportVarInfo.push_back( |
| 535 | llvm::MDString::get( |
| 536 | mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET) |
| 537 | ->getPointeeType()->getName()).c_str())); |
| 538 | break; |
| 539 | } |
| 540 | case RSExportType::ExportClassMatrix: { |
| 541 | ExportVarInfo.push_back( |
| 542 | llvm::MDString::get( |
| 543 | mLLVMContext, llvm::utostr_32( |
| 544 | /* TODO Strange value. This pushes just a number, quite |
| 545 | * different than the other cases. What is this used for? |
| 546 | * These are the metadata values that some partner drivers |
| 547 | * want to reference (for TBAA, etc.). We may want to look |
| 548 | * at whether these provide any reasonable value (or have |
| 549 | * distinct enough values to actually depend on). |
| 550 | */ |
| 551 | DataTypeRSMatrix2x2 + |
| 552 | static_cast<const RSExportMatrixType*>(ET)->getDim() - 2))); |
| 553 | break; |
| 554 | } |
| 555 | case RSExportType::ExportClassVector: |
| 556 | case RSExportType::ExportClassConstantArray: |
| 557 | case RSExportType::ExportClassRecord: { |
| 558 | ExportVarInfo.push_back( |
| 559 | llvm::MDString::get(mLLVMContext, |
| 560 | EV->getType()->getName().c_str())); |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | mExportVarMetadata->addOperand( |
| 566 | llvm::MDNode::get(mLLVMContext, ExportVarInfo)); |
| 567 | ExportVarInfo.clear(); |
| 568 | |
| 569 | if (mRSObjectSlotsMetadata == nullptr) { |
| 570 | mRSObjectSlotsMetadata = |
| 571 | M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN); |
| 572 | } |
| 573 | |
| 574 | if (countsAsRSObject) { |
| 575 | mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext, |
| 576 | llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount)))); |
| 577 | } |
| 578 | |
| 579 | slotCount++; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void Backend::dumpExportFunctionInfo(llvm::Module *M) { |
| 584 | if (mExportFuncMetadata == nullptr) |
| 585 | mExportFuncMetadata = |
| 586 | M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN); |
| 587 | |
| 588 | llvm::SmallVector<llvm::Metadata *, 1> ExportFuncInfo; |
| 589 | |
| 590 | for (RSContext::const_export_func_iterator |
| 591 | I = mContext->export_funcs_begin(), |
| 592 | E = mContext->export_funcs_end(); |
| 593 | I != E; |
| 594 | I++) { |
| 595 | const RSExportFunc *EF = *I; |
| 596 | |
| 597 | // Function name |
| 598 | if (!EF->hasParam()) { |
| 599 | ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 600 | EF->getName().c_str())); |
| 601 | } else { |
| 602 | llvm::Function *F = M->getFunction(EF->getName()); |
| 603 | llvm::Function *HelperFunction; |
| 604 | const std::string HelperFunctionName(".helper_" + EF->getName()); |
| 605 | |
| 606 | slangAssert(F && "Function marked as exported disappeared in Bitcode"); |
| 607 | |
| 608 | // Create helper function |
| 609 | { |
| 610 | llvm::StructType *HelperFunctionParameterTy = nullptr; |
| 611 | std::vector<bool> isStructInput; |
| 612 | |
| 613 | if (!F->getArgumentList().empty()) { |
| 614 | std::vector<llvm::Type*> HelperFunctionParameterTys; |
| 615 | for (llvm::Function::arg_iterator AI = F->arg_begin(), |
| 616 | AE = F->arg_end(); AI != AE; AI++) { |
| 617 | if (AI->getType()->isPointerTy() && AI->getType()->getPointerElementType()->isStructTy()) { |
| 618 | HelperFunctionParameterTys.push_back(AI->getType()->getPointerElementType()); |
| 619 | isStructInput.push_back(true); |
| 620 | } else { |
| 621 | HelperFunctionParameterTys.push_back(AI->getType()); |
| 622 | isStructInput.push_back(false); |
| 623 | } |
| 624 | } |
| 625 | HelperFunctionParameterTy = |
| 626 | llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys); |
| 627 | } |
| 628 | |
| 629 | if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) { |
| 630 | fprintf(stderr, "Failed to export function %s: parameter type " |
| 631 | "mismatch during creation of helper function.\n", |
| 632 | EF->getName().c_str()); |
| 633 | |
| 634 | const RSExportRecordType *Expected = EF->getParamPacketType(); |
| 635 | if (Expected) { |
| 636 | fprintf(stderr, "Expected:\n"); |
| 637 | Expected->getLLVMType()->dump(); |
| 638 | } |
| 639 | if (HelperFunctionParameterTy) { |
| 640 | fprintf(stderr, "Got:\n"); |
| 641 | HelperFunctionParameterTy->dump(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | std::vector<llvm::Type*> Params; |
| 646 | if (HelperFunctionParameterTy) { |
| 647 | llvm::PointerType *HelperFunctionParameterTyP = |
| 648 | llvm::PointerType::getUnqual(HelperFunctionParameterTy); |
| 649 | Params.push_back(HelperFunctionParameterTyP); |
| 650 | } |
| 651 | |
| 652 | llvm::FunctionType * HelperFunctionType = |
| 653 | llvm::FunctionType::get(F->getReturnType(), |
| 654 | Params, |
| 655 | /* IsVarArgs = */false); |
| 656 | |
| 657 | HelperFunction = |
| 658 | llvm::Function::Create(HelperFunctionType, |
| 659 | llvm::GlobalValue::ExternalLinkage, |
| 660 | HelperFunctionName, |
| 661 | M); |
| 662 | |
| 663 | HelperFunction->addFnAttr(llvm::Attribute::NoInline); |
| 664 | HelperFunction->setCallingConv(F->getCallingConv()); |
| 665 | |
| 666 | // Create helper function body |
| 667 | { |
| 668 | llvm::Argument *HelperFunctionParameter = |
| 669 | &(*HelperFunction->arg_begin()); |
| 670 | llvm::BasicBlock *BB = |
| 671 | llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction); |
| 672 | llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB); |
| 673 | llvm::SmallVector<llvm::Value*, 6> Params; |
| 674 | llvm::Value *Idx[2]; |
| 675 | |
| 676 | Idx[0] = |
| 677 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0); |
| 678 | |
| 679 | // getelementptr and load instruction for all elements in |
| 680 | // parameter .p |
| 681 | for (size_t i = 0; i < EF->getNumParameters(); i++) { |
| 682 | // getelementptr |
| 683 | Idx[1] = llvm::ConstantInt::get( |
| 684 | llvm::Type::getInt32Ty(mLLVMContext), i); |
| 685 | |
| 686 | llvm::Value *Ptr = NULL; |
| 687 | |
| 688 | Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter, Idx); |
| 689 | |
| 690 | // Load is only required for non-struct ptrs |
| 691 | if (isStructInput[i]) { |
| 692 | Params.push_back(Ptr); |
| 693 | } else { |
| 694 | llvm::Value *V = IB->CreateLoad(Ptr); |
| 695 | Params.push_back(V); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Call and pass the all elements as parameter to F |
| 700 | llvm::CallInst *CI = IB->CreateCall(F, Params); |
| 701 | |
| 702 | CI->setCallingConv(F->getCallingConv()); |
| 703 | |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 704 | if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext)) { |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 705 | IB->CreateRetVoid(); |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 706 | } else { |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 707 | IB->CreateRet(CI); |
Yang Ni | 45aa8c5 | 2015-09-14 14:55:20 -0700 | [diff] [blame^] | 708 | } |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 709 | |
| 710 | delete IB; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | ExportFuncInfo.push_back( |
| 715 | llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str())); |
| 716 | } |
| 717 | |
| 718 | mExportFuncMetadata->addOperand( |
| 719 | llvm::MDNode::get(mLLVMContext, ExportFuncInfo)); |
| 720 | ExportFuncInfo.clear(); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | void Backend::dumpExportForEachInfo(llvm::Module *M) { |
| 725 | if (mExportForEachNameMetadata == nullptr) { |
| 726 | mExportForEachNameMetadata = |
| 727 | M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_NAME_MN); |
| 728 | } |
| 729 | if (mExportForEachSignatureMetadata == nullptr) { |
| 730 | mExportForEachSignatureMetadata = |
| 731 | M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN); |
| 732 | } |
| 733 | |
| 734 | llvm::SmallVector<llvm::Metadata *, 1> ExportForEachName; |
| 735 | llvm::SmallVector<llvm::Metadata *, 1> ExportForEachInfo; |
| 736 | |
| 737 | for (RSContext::const_export_foreach_iterator |
| 738 | I = mContext->export_foreach_begin(), |
| 739 | E = mContext->export_foreach_end(); |
| 740 | I != E; |
| 741 | I++) { |
| 742 | const RSExportForEach *EFE = *I; |
| 743 | |
| 744 | ExportForEachName.push_back( |
| 745 | llvm::MDString::get(mLLVMContext, EFE->getName().c_str())); |
| 746 | |
| 747 | mExportForEachNameMetadata->addOperand( |
| 748 | llvm::MDNode::get(mLLVMContext, ExportForEachName)); |
| 749 | ExportForEachName.clear(); |
| 750 | |
| 751 | ExportForEachInfo.push_back( |
| 752 | llvm::MDString::get(mLLVMContext, |
| 753 | llvm::utostr_32(EFE->getSignatureMetadata()))); |
| 754 | |
| 755 | mExportForEachSignatureMetadata->addOperand( |
| 756 | llvm::MDNode::get(mLLVMContext, ExportForEachInfo)); |
| 757 | ExportForEachInfo.clear(); |
| 758 | } |
| 759 | } |
| 760 | |
Matt Wala | c0c5dd8 | 2015-07-23 17:29:37 -0700 | [diff] [blame] | 761 | void Backend::dumpExportReduceInfo(llvm::Module *M) { |
| 762 | if (!mExportReduceMetadata) { |
| 763 | mExportReduceMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_REDUCE_MN); |
| 764 | } |
| 765 | |
| 766 | llvm::SmallVector<llvm::Metadata *, 1> ExportReduceInfo; |
| 767 | |
| 768 | // Add the names of the reduce-style kernel functions to the metadata node. |
| 769 | for (auto I = mContext->export_reduce_begin(), |
| 770 | E = mContext->export_reduce_end(); I != E; ++I) { |
| 771 | ExportReduceInfo.clear(); |
| 772 | |
| 773 | ExportReduceInfo.push_back( |
| 774 | llvm::MDString::get(mLLVMContext, (*I)->getName().c_str())); |
| 775 | |
| 776 | mExportReduceMetadata->addOperand( |
| 777 | llvm::MDNode::get(mLLVMContext, ExportReduceInfo)); |
| 778 | } |
| 779 | } |
| 780 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 781 | void Backend::dumpExportTypeInfo(llvm::Module *M) { |
| 782 | llvm::SmallVector<llvm::Metadata *, 1> ExportTypeInfo; |
| 783 | |
| 784 | for (RSContext::const_export_type_iterator |
| 785 | I = mContext->export_types_begin(), |
| 786 | E = mContext->export_types_end(); |
| 787 | I != E; |
| 788 | I++) { |
| 789 | // First, dump type name list to export |
| 790 | const RSExportType *ET = I->getValue(); |
| 791 | |
| 792 | ExportTypeInfo.clear(); |
| 793 | // Type name |
| 794 | ExportTypeInfo.push_back( |
| 795 | llvm::MDString::get(mLLVMContext, ET->getName().c_str())); |
| 796 | |
| 797 | if (ET->getClass() == RSExportType::ExportClassRecord) { |
| 798 | const RSExportRecordType *ERT = |
| 799 | static_cast<const RSExportRecordType*>(ET); |
| 800 | |
| 801 | if (mExportTypeMetadata == nullptr) |
| 802 | mExportTypeMetadata = |
| 803 | M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN); |
| 804 | |
| 805 | mExportTypeMetadata->addOperand( |
| 806 | llvm::MDNode::get(mLLVMContext, ExportTypeInfo)); |
| 807 | |
| 808 | // Now, export struct field information to %[struct name] |
| 809 | std::string StructInfoMetadataName("%"); |
| 810 | StructInfoMetadataName.append(ET->getName()); |
| 811 | llvm::NamedMDNode *StructInfoMetadata = |
| 812 | M->getOrInsertNamedMetadata(StructInfoMetadataName); |
| 813 | llvm::SmallVector<llvm::Metadata *, 3> FieldInfo; |
| 814 | |
| 815 | slangAssert(StructInfoMetadata->getNumOperands() == 0 && |
| 816 | "Metadata with same name was created before"); |
| 817 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
| 818 | FE = ERT->fields_end(); |
| 819 | FI != FE; |
| 820 | FI++) { |
| 821 | const RSExportRecordType::Field *F = *FI; |
| 822 | |
| 823 | // 1. field name |
| 824 | FieldInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 825 | F->getName().c_str())); |
| 826 | |
| 827 | // 2. field type name |
| 828 | FieldInfo.push_back( |
| 829 | llvm::MDString::get(mLLVMContext, |
| 830 | F->getType()->getName().c_str())); |
| 831 | |
| 832 | StructInfoMetadata->addOperand( |
| 833 | llvm::MDNode::get(mLLVMContext, FieldInfo)); |
| 834 | FieldInfo.clear(); |
| 835 | } |
| 836 | } // ET->getClass() == RSExportType::ExportClassRecord |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | void Backend::HandleTranslationUnitPost(llvm::Module *M) { |
| 841 | |
| 842 | if (!mContext->is64Bit()) { |
| 843 | M->setDataLayout("e-p:32:32-i64:64-v128:64:128-n32-S64"); |
| 844 | } |
| 845 | |
| 846 | if (!mContext->processExport()) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | if (mContext->hasExportVar()) |
| 851 | dumpExportVarInfo(M); |
| 852 | |
| 853 | if (mContext->hasExportFunc()) |
| 854 | dumpExportFunctionInfo(M); |
| 855 | |
| 856 | if (mContext->hasExportForEach()) |
| 857 | dumpExportForEachInfo(M); |
| 858 | |
Matt Wala | c0c5dd8 | 2015-07-23 17:29:37 -0700 | [diff] [blame] | 859 | if (mContext->hasExportReduce()) |
| 860 | dumpExportReduceInfo(M); |
| 861 | |
Jean-Luc Brouillet | 8024ed5 | 2015-05-04 23:02:25 -0700 | [diff] [blame] | 862 | if (mContext->hasExportType()) |
| 863 | dumpExportTypeInfo(M); |
| 864 | } |
| 865 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 866 | } // namespace slang |