109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo/*
209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * Copyright 2015, The Android Open Source Project
309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo *
409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * Licensed under the Apache License, Version 2.0 (the "License");
509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * you may not use this file except in compliance with the License.
609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * You may obtain a copy of the License at
709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo *
809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo *     http://www.apache.org/licenses/LICENSE-2.0
909c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo *
1009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * Unless required by applicable law or agreed to in writing, software
1109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * distributed under the License is distributed on an "AS IS" BASIS,
1209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * See the License for the specific language governing permissions and
1409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * limitations under the License.
1509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo */
1609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
1709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include "bcc/Assert.h"
1809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include "bcc/Renderscript/RSTransforms.h"
192351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo#include "bcc/Support/Log.h"
202351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo#include "bcinfo/MetadataExtractor.h"
2109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
2209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/Pass.h>
2309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/IR/DIBuilder.h>
2409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/IR/Function.h>
2509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/IR/InstIterator.h>
2609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/IR/Instructions.h>
272351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo#include <llvm/IR/IRBuilder.h>
2809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo#include <llvm/IR/Module.h>
2909c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
3009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leonamespace {
3109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
3209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leoconst char DEBUG_SOURCE_PATH[] = "/opt/renderscriptdebugger/1";
3309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leoconst char DEBUG_GENERATED_FILE[] = "generated.rs";
342351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leoconst char DEBUG_PROTOTYPE_VAR_NAME[] = "rsDebugOuterForeachT";
352351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leoconst char DEBUG_COMPILE_UNIT_MDNAME[] = "llvm.dbg.cu";
3609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
3709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo/*
3809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * LLVM pass to attach debug information to the bits of code
3909c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo * generated by the compiler.
4009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo */
4109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leoclass RSAddDebugInfoPass : public llvm::ModulePass {
4209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
4309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leopublic:
4409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  // Pass ID
4509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  static char ID;
4609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
472351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  RSAddDebugInfoPass() : ModulePass(ID), kernelTypeMD(nullptr),
482351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      sourceFileName(nullptr), emptyExpr(nullptr), abiMetaCU(nullptr),
492351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      indexVarType(nullptr) {
5009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  }
5109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
5209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  virtual bool runOnModule(llvm::Module &Module) {
532351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Gather information about this bcc module.
542351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    bcinfo::MetadataExtractor me(&Module);
552351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (!me.extract()) {
562351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      ALOGE("Could not extract metadata from module!");
572351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      return false;
582351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
592351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
602351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    size_t nForEachKernels = me.getExportForEachSignatureCount();
612351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    const char **forEachKernels = me.getExportForEachNameList();
622351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
6309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    // Set up the debug info builder.
6409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    llvm::DIBuilder DebugInfo(Module);
652351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
662351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    initializeDebugInfo(DebugInfo, Module);
6709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
6809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    // Attach DI metadata to each generated function.
692351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    for (size_t i = 0; i < nForEachKernels; ++i) {
702351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      std::string expandedName = forEachKernels[i];
712351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      expandedName += ".expand";
722351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
732351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      if (llvm::Function *kernelFunc = Module.getFunction(expandedName))
742351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        attachDebugInfo(DebugInfo, *kernelFunc);
752351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
7609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
7709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    DebugInfo.finalize();
7809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
792351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    cleanupDebugInfo(Module);
802351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
8109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    return true;
8209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  }
8309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
8409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leoprivate:
8509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
862351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // @brief Initialize the debug info generation.
872351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  //
882351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // This method does a couple of things:
892351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // * Look up debug metadata for kernel ABI and store it if present.
902351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // * Store a couple of useful pieces of debug metadata in member
912351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  //   variables so they do not have to be created multiple times.
922351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  void initializeDebugInfo(llvm::DIBuilder &DebugInfo,
932351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo                           const llvm::Module &Module) {
942351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::LLVMContext &ctx = Module.getContext();
952351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
962351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Start generating debug information for bcc-generated code.
972351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    DebugInfo.createCompileUnit(llvm::dwarf::DW_LANG_C99,
982351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo                                DEBUG_GENERATED_FILE, DEBUG_SOURCE_PATH,
992351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo                                "RS", false, "", 0);
1002351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1012351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Pre-generate and save useful pieces of debug metadata.
1022351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    sourceFileName = DebugInfo.createFile(DEBUG_GENERATED_FILE, DEBUG_SOURCE_PATH);
1032351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    emptyExpr = DebugInfo.createExpression();
1042351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1052351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Lookup compile unit with kernel ABI debug metadata.
1062351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::NamedMDNode *mdCompileUnitList =
1072351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        Module.getNamedMetadata(DEBUG_COMPILE_UNIT_MDNAME);
1082351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    bccAssert(mdCompileUnitList != nullptr &&
1092351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        "DebugInfo pass could not find any existing compile units.");
1102351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1112351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::DIGlobalVariable *kernelPrototypeVarMD = nullptr;
1122351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    for (llvm::MDNode* CUNode : mdCompileUnitList->operands()) {
1132351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      if (auto *CU = llvm::dyn_cast<llvm::DICompileUnit>(CUNode)) {
1142351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        for (llvm::DIGlobalVariable* GV : CU->getGlobalVariables()) {
1152351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          if (GV->getDisplayName() == DEBUG_PROTOTYPE_VAR_NAME) {
1162351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo            kernelPrototypeVarMD = GV;
1172351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo            abiMetaCU = CU;
1182351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo            break;
1192351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          }
1202351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        }
1212351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        if (kernelPrototypeVarMD != nullptr)
1222351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          break;
1232351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      }
1242351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
1252351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1262351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Lookup the expanded function interface type metadata.
1272351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::MDTuple *kernelPrototypeMD = nullptr;
1282351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (kernelPrototypeVarMD != nullptr) {
1292351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Dig into the metadata to look for function prototype.
1302351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::DIDerivedType *DT = nullptr;
1312351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      DT = llvm::cast<llvm::DIDerivedType>(kernelPrototypeVarMD->getType());
1322351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      DT = llvm::cast<llvm::DIDerivedType>(DT->getBaseType());
1332351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::DISubroutineType *ST = llvm::cast<llvm::DISubroutineType>(DT->getBaseType());
1342351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      kernelPrototypeMD = llvm::cast<llvm::MDTuple>(ST->getRawTypeArray());
1352351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1362351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      indexVarType = llvm::dyn_cast_or_null<llvm::DIType>(
1372351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          kernelPrototypeMD->getOperand(2));
1382351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
1392351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Fall back to the function type of void() if there is no proper debug info.
1402351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (kernelPrototypeMD == nullptr)
1412351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      kernelPrototypeMD = llvm::MDTuple::get(ctx, {nullptr});
1422351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Fall back to unspecified type if we don't have a proper index type.
1432351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (indexVarType == nullptr)
1442351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      indexVarType = DebugInfo.createBasicType("uint32_t", 32, 32,
1452351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          llvm::dwarf::DW_ATE_unsigned);
1462351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1472351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Capture the expanded kernel type debug info.
1488e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar    kernelTypeMD = DebugInfo.createSubroutineType(kernelPrototypeMD);
1492351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  }
1502351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
15109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  /// @brief Add debug information to a generated function.
15209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  ///
1532351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// This procedure adds the following pieces of debug information
1542351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// to the function specified by Func:
1552351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// * Entry for the function to the current compile unit.
1562351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// * Adds debug info entries for each function argument.
1572351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// * Adds debug info entry for the rsIndex local variable.
1582351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  /// * File/line information to each instruction set to generates.rs:1.
15909c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  void attachDebugInfo(llvm::DIBuilder &DebugInfo, llvm::Function &Func) {
1602351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Lookup the current thread coordinate variable.
1612351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::AllocaInst* indexVar = nullptr;
1628e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar    for (llvm::Instruction &inst : llvm::instructions(Func)) {
1632351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      if (auto *allocaInst = llvm::dyn_cast<llvm::AllocaInst>(&inst)) {
1642351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        if (allocaInst->getName() == bcc::BCC_INDEX_VAR_NAME) {
1652351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          indexVar = allocaInst;
1662351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          break;
1672351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        }
1682351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      }
1692351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
17009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
17109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    // Create function-level debug metadata.
1722351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::DISubprogram *ExpandedFunc = DebugInfo.createFunction(
17309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo        sourceFileName, // scope
17409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo        Func.getName(), Func.getName(),
1752351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        sourceFileName, 1, kernelTypeMD,
1768e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar        false, true, 1, 0, false
17709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    );
17809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
1792351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // IRBuilder for allocating variables for arguments.
1808e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar    llvm::IRBuilder<> ir(&*Func.getEntryBlock().begin());
1812351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1822351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Walk through the argument list and expanded function prototype
1832351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // debuginfo in lockstep to create debug entries for
1842351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // the expanded function arguments.
1852351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    unsigned argIdx = 1;
1862351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::MDTuple *argTypes = kernelTypeMD->getTypeArray().get();
1872351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    for (llvm::Argument &arg : Func.getArgumentList()) {
1882351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Stop processing arguments if we run out of debug info.
1892351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      if (argIdx >= argTypes->getNumOperands())
1902351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        break;
1912351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1922351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Create debuginfo entry for the argument and advance.
1938e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar      llvm::DILocalVariable *argVarDI = DebugInfo.createParameterVariable(
1948e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar          ExpandedFunc, arg.getName(), argIdx, sourceFileName, 1,
1952351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          llvm::cast<llvm::DIType>(argTypes->getOperand(argIdx).get()),
1968e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar          true, 0
1978e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar      );
1982351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
1992351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Annotate the argument variable in the IR.
2002351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::AllocaInst *argVar =
2012351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          ir.CreateAlloca(arg.getType(), nullptr, arg.getName() + ".var");
2022351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::StoreInst *argStore = ir.CreateStore(&arg, argVar);
2032351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::LoadInst *loadedVar = ir.CreateLoad(argVar, arg.getName() + ".l");
2042351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      DebugInfo.insertDeclare(argVar, argVarDI, emptyExpr,
2052351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          llvm::DebugLoc::get(1, 1, ExpandedFunc), loadedVar);
2062351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      for (llvm::Use &u : arg.uses())
2072351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        if (u.getUser() != argStore)
2082351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          u.set(loadedVar);
2092351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      argIdx++;
2102351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
2112351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
2122351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Annotate the index variable with metadata.
2132351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (indexVar) {
2142351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Debug information for loop index variable.
2158e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar      llvm::DILocalVariable *indexVarDI = DebugInfo.createAutoVariable(
2168e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar          ExpandedFunc, bcc::BCC_INDEX_VAR_NAME, sourceFileName, 1,
2178e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar          indexVarType, true
2188e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar      );
2192351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
2202351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      // Insert declaration annotation in the instruction stream.
2212351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      llvm::Instruction *decl = DebugInfo.insertDeclare(
2222351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          indexVar, indexVarDI, emptyExpr,
2232351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo          llvm::DebugLoc::get(1, 1, ExpandedFunc), indexVar);
2242351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      indexVar->moveBefore(decl);
2252351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    }
2262351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
2272351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Attach location information to each instruction in the function.
2288e9089377848628813a697b972773e969b942c3bPirama Arumuga Nainar    for (llvm::Instruction &inst : llvm::instructions(Func)) {
2292351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      inst.setDebugLoc(llvm::DebugLoc::get(1, 1, ExpandedFunc));
23009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo    }
23109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  }
23209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
2332351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // @brief Clean up the debug info.
2342351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  //
2352351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // At the moment, it only finds the compile unit for the expanded function
2362351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // metadata generated by clang and removes it.
2372351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  void cleanupDebugInfo(llvm::Module& Module) {
2382351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    if (abiMetaCU == nullptr)
2392351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      return;
2402351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
2412351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    // Remove the compile unit with the runtime interface DI.
2422351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::SmallVector<llvm::MDNode*, 4> unitsTmp;
2432351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    llvm::NamedMDNode *debugMD =
2442351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        Module.getNamedMetadata(DEBUG_COMPILE_UNIT_MDNAME);
2452351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    for (llvm::MDNode *cu : debugMD->operands())
2462351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      if (cu != abiMetaCU)
2472351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo        unitsTmp.push_back(cu);
2482351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    debugMD->eraseFromParent();
2492351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    debugMD = Module.getOrInsertNamedMetadata(DEBUG_COMPILE_UNIT_MDNAME);
2502351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo    for (llvm::MDNode *cu : unitsTmp)
2512351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo      debugMD->addOperand(cu);
2522351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  }
2532351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
2542351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leoprivate:
2552351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  // private attributes
2562351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  llvm::DISubroutineType* kernelTypeMD;
2572351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  llvm::DIFile *sourceFileName;
2582351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  llvm::DIExpression *emptyExpr;
2592351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  llvm::DICompileUnit *abiMetaCU;
2602351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo  llvm::DIType *indexVarType;
2612351f9290cb9de67e5afb79b2afc7408bf4eb926Dean De Leo
26209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo}; // end class RSAddDebugInfoPass
26309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
26409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leochar RSAddDebugInfoPass::ID = 0;
26509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leostatic llvm::RegisterPass<RSAddDebugInfoPass> X("addrsdi", "Add RS DebugInfo Pass");
26609c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
26709c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo} // end anonymous namespace
26809c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
26909c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leonamespace bcc {
27009c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
27109c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leollvm::ModulePass * createRSAddDebugInfoPass() {
27209c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo  return new RSAddDebugInfoPass();
27309c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo}
27409c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo
27509c7a41f73602bec33e9d392cc959d78931f73c4Dean De Leo} // end namespace bcc
276