[llvm-bugs] [Bug 36183] New: Invoke a Function Pass in clang getting: `use_empty() && Uses remain when value is destriyed!"', function ~Value

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 31 15:20:43 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=36183

            Bug ID: 36183
           Summary: Invoke a Function Pass in clang getting: `use_empty()
                    && Uses remain when value is destriyed!"', function
                    ~Value
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: golamkayas at temple.edu
                CC: llvm-bugs at lists.llvm.org

Created attachment 19784
  --> https://bugs.llvm.org/attachment.cgi?id=19784&action=edit
LLVM IR file

Hi, 
I build LLVM from the trunk on 30 January 2018. 
I write a simple function pass try to instrument a thread-local global
variable. And the value of the variable before instruction.

To build the pass, I put my pass folder under <llvmm_source>/lib/Transform and
made the necessary changes in the CMakeList file

When I try to invoke the pass using "opt" like below:

clang -O0 -emit-llvm test.c -c -o test_unoptimized.bc
opt -load $OBJ_ROOT/lib/SimplePass.so -instrument < test_unoptimized.bc >
test_unoptimized_new.bc
clang -g test_unoptimized_new.bc -o test_unoptimized_new

It works fine. But whenever I tried to invoke the pass without the involvement
of
like below:
clang  -Xclang -load -Xclang $LIB/SimplePass.so -O0 s  test.c -o test

I got the "use_empty()" error.

My pass looks like below:


#include "llvm/IR/Function.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include <boost/functional/hash.hpp>
#include <tr1/functional>

using namespace llvm;

namespace {
struct SimplePass : public FunctionPass {
public:
  static char ID;
  SimplePass() : FunctionPass(ID) {}

  virtual bool runOnFunction(Function &F) {
    static LLVMContext ctx;

    Module *parentModule = F.getParent();
    GlobalVariable *gPCC;
    Type *Int32Type = IntegerType::getInt32Ty(ctx);

    BasicBlock &entryBlock = F.getEntryBlock();
    Instruction *instPt = &entryBlock.front();
    IRBuilder<> B(instPt);
    LoadInst *LI;

    Constant *value;

    Constant *zero = ConstantInt::get(Int32Type, (uint64_t)0, false);
    unsigned int callsite_index = 0;

    if (!parentModule->getGlobalVariable("t_pcc")) {
      gPCC = new GlobalVariable(
                        *parentModule,
                        Int32Type,
                        false,
                        GlobalValue::CommonLinkage,
                        zero,
                        "t_pcc",
                        0,
                        GlobalVariable::InitialExecTLSModel);
                        gPCC->setAlignment(4);
    } else {
      gPCC = parentModule->getGlobalVariable("t_pcc");
    }

    LI = B.CreateLoad(gPCC);

    for (auto &BB : F) {
      for (auto &I : BB) {
        callsite_index++;
        if (auto *callInst = dyn_cast<CallInst>(&I)) {

          IRBuilder<> callSiteBuilder(callInst);

          value = ConstantInt::get(Int32Type, 10);

          callSiteBuilder.CreateStore(value, gPCC);
        }
      }
    }
    errs() << "Instrumented Function: " << F.getName() << "\n";
    return true;
  }

};
} // namespace

char SimplePass::ID = 0;


namespace{
static RegisterPass<SimplePass> X("instrument", "Instrumentaion Pass", false,
false);
}

static void registerPCCSimple(const PassManagerBuilder &,
                                 legacy::PassManagerBase &PM) {
  PM.add(new SimplePass());
}
static RegisterStandardPasses
    RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
                   registerPCCSimple);




//End of the pass

I am attaching my pass and my LLVM IR(.i) file.
Thanks.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180131/4c1a6aa2/attachment.html>


More information about the llvm-bugs mailing list