1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
//===- DebugifyTest.cpp - Debugify unit tests -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallVector.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Utils/Debugify.h"
#include "gtest/gtest.h"
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("DebugifyTest", errs());
return Mod;
}
namespace llvm {
void initializeDebugInfoDropPass(PassRegistry &);
void initializeDebugInfoDummyAnalysisPass(PassRegistry &);
namespace {
struct DebugInfoDrop : public FunctionPass {
static char ID;
bool runOnFunction(Function &F) override {
// Drop DISubprogram.
F.setSubprogram(nullptr);
for (BasicBlock &BB : F) {
// Remove debug locations.
for (Instruction &I : BB)
I.setDebugLoc(DebugLoc());
}
return false;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
}
DebugInfoDrop() : FunctionPass(ID) {}
};
struct DebugValueDrop : public FunctionPass {
static char ID;
bool runOnFunction(Function &F) override {
SmallVector<DbgVariableIntrinsic *, 4> Dbgs;
for (BasicBlock &BB : F) {
// Remove dbg var intrinsics.
for (Instruction &I : BB) {
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
Dbgs.push_back(DVI);
}
}
for (auto &I : Dbgs)
I->eraseFromParent();
return true;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
}
DebugValueDrop() : FunctionPass(ID) {}
};
struct DebugInfoDummyAnalysis : public FunctionPass {
static char ID;
bool runOnFunction(Function &F) override {
// Do nothing, so debug info stays untouched.
return false;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
}
DebugInfoDummyAnalysis() : FunctionPass(ID) {}
};
}
char DebugInfoDrop::ID = 0;
char DebugValueDrop::ID = 0;
char DebugInfoDummyAnalysis::ID = 0;
TEST(DebugInfoDrop, DropOriginalDebugInfo) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"(
define i16 @f(i16 %a) !dbg !6 {
%b = add i16 %a, 1, !dbg !11
call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
ret i16 0, !dbg !11
}
declare void @llvm.dbg.value(metadata, metadata, metadata)
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!5}
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "t.ll", directory: "/")
!2 = !{}
!5 = !{i32 2, !"Debug Info Version", i32 3}
!6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!7 = !DISubroutineType(types: !2)
!8 = !{!9}
!9 = !DILocalVariable(name: "b", scope: !6, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocation(line: 1, column: 1, scope: !6)
)");
DebugInfoDrop *P = new DebugInfoDrop();
DebugInfoPerPassMap DIPreservationMap;
DebugifyCustomPassManager Passes;
Passes.setDIPreservationMap(DIPreservationMap);
Passes.add(createDebugifyModulePass(DebugifyMode::OriginalDebugInfo, "",
&(Passes.getDebugInfoPerPassMap())));
Passes.add(P);
Passes.add(createCheckDebugifyModulePass(false, "", nullptr,
DebugifyMode::OriginalDebugInfo,
&(Passes.getDebugInfoPerPassMap())));
testing::internal::CaptureStderr();
Passes.run(*M);
std::string StdOut = testing::internal::GetCapturedStderr();
std::string ErrorForSP = "ERROR: dropped DISubprogram of";
std::string WarningForLoc = "WARNING: dropped DILocation of";
std::string FinalResult = "CheckModuleDebugify (original debuginfo): FAIL";
EXPECT_TRUE(StdOut.find(ErrorForSP) != std::string::npos);
EXPECT_TRUE(StdOut.find(WarningForLoc) != std::string::npos);
EXPECT_TRUE(StdOut.find(FinalResult) != std::string::npos);
}
TEST(DebugValueDrop, DropOriginalDebugValues) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"(
define i16 @f(i16 %a) !dbg !6 {
%b = add i16 %a, 1, !dbg !11
call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
ret i16 0, !dbg !11
}
declare void @llvm.dbg.value(metadata, metadata, metadata)
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!5}
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "t.ll", directory: "/")
!2 = !{}
!5 = !{i32 2, !"Debug Info Version", i32 3}
!6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!7 = !DISubroutineType(types: !2)
!8 = !{!9}
!9 = !DILocalVariable(name: "b", scope: !6, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocation(line: 1, column: 1, scope: !6)
)");
DebugValueDrop *P = new DebugValueDrop();
DebugInfoPerPassMap DIPreservationMap;
DebugifyCustomPassManager Passes;
Passes.setDIPreservationMap(DIPreservationMap);
Passes.add(createDebugifyModulePass(DebugifyMode::OriginalDebugInfo, "",
&(Passes.getDebugInfoPerPassMap())));
Passes.add(P);
Passes.add(createCheckDebugifyModulePass(false, "", nullptr,
DebugifyMode::OriginalDebugInfo,
&(Passes.getDebugInfoPerPassMap())));
testing::internal::CaptureStderr();
Passes.run(*M);
std::string StdOut = testing::internal::GetCapturedStderr();
std::string ErrorForSP = "ERROR: dropped DISubprogram of";
std::string WarningForLoc = "WARNING: dropped DILocation of";
std::string WarningForVars = "WARNING: drops dbg.value()/dbg.declare() for";
std::string FinalResult = "CheckModuleDebugify (original debuginfo): FAIL";
EXPECT_TRUE(StdOut.find(ErrorForSP) == std::string::npos);
EXPECT_TRUE(StdOut.find(WarningForLoc) == std::string::npos);
EXPECT_TRUE(StdOut.find(WarningForVars) != std::string::npos);
EXPECT_TRUE(StdOut.find(FinalResult) != std::string::npos);
}
TEST(DebugInfoDummyAnalysis, PreserveOriginalDebugInfo) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"(
define i32 @g(i32 %b) !dbg !6 {
%c = add i32 %b, 1, !dbg !11
call void @llvm.dbg.value(metadata i32 %c, metadata !9, metadata !DIExpression()), !dbg !11
ret i32 1, !dbg !11
}
declare void @llvm.dbg.value(metadata, metadata, metadata)
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!5}
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "test.ll", directory: "/")
!2 = !{}
!5 = !{i32 2, !"Debug Info Version", i32 3}
!6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!7 = !DISubroutineType(types: !2)
!8 = !{!9}
!9 = !DILocalVariable(name: "c", scope: !6, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
!11 = !DILocation(line: 1, column: 1, scope: !6)
)");
DebugInfoDummyAnalysis *P = new DebugInfoDummyAnalysis();
DebugInfoPerPassMap DIPreservationMap;
DebugifyCustomPassManager Passes;
Passes.setDIPreservationMap(DIPreservationMap);
Passes.add(createDebugifyModulePass(DebugifyMode::OriginalDebugInfo, "",
&(Passes.getDebugInfoPerPassMap())));
Passes.add(P);
Passes.add(createCheckDebugifyModulePass(false, "", nullptr,
DebugifyMode::OriginalDebugInfo,
&(Passes.getDebugInfoPerPassMap())));
testing::internal::CaptureStderr();
Passes.run(*M);
std::string StdOut = testing::internal::GetCapturedStderr();
std::string ErrorForSP = "ERROR: dropped DISubprogram of";
std::string WarningForLoc = "WARNING: dropped DILocation of";
std::string WarningForVars = "WARNING: drops dbg.value()/dbg.declare() for";
std::string FinalResult = "CheckModuleDebugify (original debuginfo): PASS";
EXPECT_TRUE(StdOut.find(ErrorForSP) == std::string::npos);
EXPECT_TRUE(StdOut.find(WarningForLoc) == std::string::npos);
EXPECT_TRUE(StdOut.find(WarningForVars) == std::string::npos);
EXPECT_TRUE(StdOut.find(FinalResult) != std::string::npos);
}
} // end namespace llvm
INITIALIZE_PASS_BEGIN(DebugInfoDrop, "debuginfodroppass", "debuginfodroppass",
false, false)
INITIALIZE_PASS_END(DebugInfoDrop, "debuginfodroppass", "debuginfodroppass", false,
false)
INITIALIZE_PASS_BEGIN(DebugInfoDummyAnalysis, "debuginfodummyanalysispass",
"debuginfodummyanalysispass", false, false)
INITIALIZE_PASS_END(DebugInfoDummyAnalysis, "debuginfodummyanalysispass",
"debuginfodummyanalysispass", false, false)
|