[PATCH] D20541: [esan|cfrag]: Add createCacheFragGV.

Qin Zhao via llvm-commits llvm-commits at lists.llvm.org
Tue May 24 09:23:34 PDT 2016


zhaoqin updated this revision to Diff 58246.
zhaoqin added a comment.

update commit msg title


http://reviews.llvm.org/D20541

Files:
  lib/Transforms/Instrumentation/EfficiencySanitizer.cpp

Index: lib/Transforms/Instrumentation/EfficiencySanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/EfficiencySanitizer.cpp
+++ lib/Transforms/Instrumentation/EfficiencySanitizer.cpp
@@ -80,6 +80,21 @@
   return Options;
 }
 
+// Create a constant for Str so that we can pass it to the run-time lib.
+static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
+                                                    bool AllowMerging) {
+  Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
+  // We use private linkage for module-local strings. If they can be merged
+  // with another one, we set the unnamed_addr attribute.
+  GlobalVariable *GV =
+    new GlobalVariable(M, StrConst->getType(), true,
+                       GlobalValue::PrivateLinkage, StrConst, "");
+  if (AllowMerging)
+    GV->setUnnamedAddr(true);
+  GV->setAlignment(1);  // Strings may not be merged w/o setting align 1.
+  return GV;
+}
+
 /// EfficiencySanitizer: instrument each module to find performance issues.
 class EfficiencySanitizer : public ModulePass {
 public:
@@ -93,6 +108,7 @@
 private:
   bool initOnModule(Module &M);
   void initializeCallbacks(Module &M);
+  GlobalVariable *createCacheFragGV(Module &M);
   GlobalVariable *createEsanInitToolGV(Module &M);
   void createDestructor(Module &M, GlobalVariable *GV);
   bool runOnFunction(Function &F, Module &M);
@@ -179,10 +195,42 @@
                             IRB.getInt32Ty(), IntptrTy, nullptr));
 }
 
+// Create the global variable for the cache-fragmentation tool.
+GlobalVariable *EfficiencySanitizer::createCacheFragGV(Module &M) {
+  assert(Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag);
+
+  // This structure contains tool-specific information about each compilation
+  // unit (module) and is passed to the runtime library.
+  // This structure should be kept consistent with the CacheFragTy in the
+  // runtime library cache_frag tool.
+  // struct CacheFragTy {
+  //   int         ToolType;
+  //   const char *UnitName;
+  //   FIXME: add more entries for cache frag tool.
+  // };
+
+  IntegerType *Int32Ty = Type::getInt32Ty(*Ctx);
+  StructType *CacheFragTy =
+    StructType::get(Int32Ty, IntptrTy, nullptr);
+
+  // Compilation unit name.
+  GlobalVariable *UnitName = createPrivateGlobalForString(
+      M, M.getModuleIdentifier(), /*AllowMerging*/ true);
+
+  GlobalVariable *CacheFragGV = new GlobalVariable(
+      M, CacheFragTy, true, GlobalVariable::InternalLinkage,
+      ConstantStruct::get(CacheFragTy,
+                          ConstantInt::get(Int32Ty, Options.ToolType),
+                          ConstantExpr::getPointerCast(UnitName, IntptrTy),
+                          nullptr));
+  return CacheFragGV;
+}
+
 // Create the tool-specific global variable passed to EsanInit and EsanExit.
 GlobalVariable *EfficiencySanitizer::createEsanInitToolGV(Module &M) {
   GlobalVariable *GV = nullptr;
-  // FIXME: create the tool specific global variable.
+  if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag)
+    GV = createCacheFragGV(M);
   if (GV == nullptr) {
     GV = new GlobalVariable(M, IntptrTy, true, GlobalVariable::InternalLinkage,
                             Constant::getNullValue(IntptrTy));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20541.58246.patch
Type: text/x-patch
Size: 3332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160524/e861978b/attachment.bin>


More information about the llvm-commits mailing list