Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 01ad79b

Browse files
committed
[GVN, OptDiag] Include the value that is forwarded in load elimination
This requires some changes to the opt-diag API. Hal and I have discussed this at the Dev Meeting and came up with a streaming delimiter (setExtraArgs) to solve this. Arguments after this delimiter are only included in the optimization records and not in the remarks printed in the compiler output. (Note, how in the test the content of the YAML file changes but the remarks on the compiler output don't.) This implements the green GVN message with a bug fix at line http://lab.llvm.org:8080/artifacts/opt-view_test-suite/build/SingleSource/Benchmarks/Dhrystone/CMakeFiles/dry.dir/html/_org_test-suite_SingleSource_Benchmarks_Dhrystone_dry.c.html#L446 The fix is that now we properly include the constant value in the message: "load of type i32 eliminated in favor of 7" Differential Revision: https://reviews.llvm.org/D26489 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288047 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e9741d2 commit 01ad79b

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

include/llvm/Analysis/OptimizationDiagnosticInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class OptimizationRemarkEmitter {
223223
namespace ore {
224224
using NV = DiagnosticInfoOptimizationBase::Argument;
225225
using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
226+
using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
226227
}
227228

228229
/// OptimizationRemarkEmitter legacy analysis pass

include/llvm/IR/DiagnosticInfo.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ class DiagnosticInfoOptimizationBase : public DiagnosticInfoWithDebugLocBase {
382382
/// \brief Used to set IsVerbose via the stream interface.
383383
struct setIsVerbose {};
384384

385+
/// \brief When an instance of this is inserted into the stream, the arguments
386+
/// following will not appear in the remark printed in the compiler output
387+
/// (-Rpass) but only in the optimization record file
388+
/// (-fsave-optimization-record).
389+
struct setExtraArgs {};
390+
385391
/// \brief Used in the streaming interface as the general argument type. It
386392
/// internally converts everything into a key-value pair.
387393
struct Argument {
@@ -452,6 +458,7 @@ class DiagnosticInfoOptimizationBase : public DiagnosticInfoWithDebugLocBase {
452458
DiagnosticInfoOptimizationBase &operator<<(StringRef S);
453459
DiagnosticInfoOptimizationBase &operator<<(Argument A);
454460
DiagnosticInfoOptimizationBase &operator<<(setIsVerbose V);
461+
DiagnosticInfoOptimizationBase &operator<<(setExtraArgs EA);
455462

456463
/// \see DiagnosticInfo::print.
457464
void print(DiagnosticPrinter &DP) const override;
@@ -501,6 +508,11 @@ class DiagnosticInfoOptimizationBase : public DiagnosticInfoWithDebugLocBase {
501508
/// The remark is expected to be noisy.
502509
bool IsVerbose = false;
503510

511+
/// \brief If positive, the index of the first argument that only appear in
512+
/// the optimization records and not in the remark printed in the compiler
513+
/// output.
514+
int FirstExtraArgIndex = -1;
515+
504516
friend struct yaml::MappingTraits<DiagnosticInfoOptimizationBase *>;
505517
};
506518

lib/IR/DiagnosticInfo.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,25 @@ const std::string DiagnosticInfoWithDebugLocBase::getLocationStr() const {
170170
getLocation(&Filename, &Line, &Column);
171171
return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
172172
}
173+
173174
DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, Value *V)
174-
: Key(Key), Val(GlobalValue::getRealLinkageName(V->getName())) {
175+
: Key(Key) {
175176
if (auto *F = dyn_cast<Function>(V)) {
176177
if (DISubprogram *SP = F->getSubprogram())
177178
DLoc = DebugLoc::get(SP->getScopeLine(), 0, SP);
178179
}
179180
else if (auto *I = dyn_cast<Instruction>(V))
180181
DLoc = I->getDebugLoc();
182+
183+
// Only include names that correspond to user variables. FIXME: we should use
184+
// debug info if available to get the name of the user variable.
185+
if (isa<llvm::Argument>(V) || isa<GlobalValue>(V))
186+
Val = GlobalValue::getRealLinkageName(V->getName());
187+
else if (isa<Constant>(V)) {
188+
raw_string_ostream OS(Val);
189+
V->printAsOperand(OS, /*PrintType=*/false);
190+
} else if (auto *I = dyn_cast<Instruction>(V))
191+
Val = I->getOpcodeName();
181192
}
182193

183194
DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, Type *T)
@@ -359,10 +370,19 @@ operator<<(setIsVerbose V) {
359370
return *this;
360371
}
361372

373+
DiagnosticInfoOptimizationBase &DiagnosticInfoOptimizationBase::
374+
operator<<(setExtraArgs EA) {
375+
FirstExtraArgIndex = Args.size();
376+
return *this;
377+
}
378+
362379
std::string DiagnosticInfoOptimizationBase::getMsg() const {
363380
std::string Str;
364381
raw_string_ostream OS(Str);
365-
for (const DiagnosticInfoOptimizationBase::Argument &Arg : Args)
382+
for (const DiagnosticInfoOptimizationBase::Argument &Arg :
383+
make_range(Args.begin(), FirstExtraArgIndex == -1
384+
? Args.end()
385+
: Args.begin() + FirstExtraArgIndex))
366386
OS << Arg.Val;
367387
return OS.str();
368388
}

lib/Transforms/Scalar/GVN.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,10 +1590,13 @@ bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
15901590
return true;
15911591
}
15921592

1593-
static void reportLoadElim(LoadInst *LI, OptimizationRemarkEmitter *ORE) {
1593+
static void reportLoadElim(LoadInst *LI, Value *AvailableValue,
1594+
OptimizationRemarkEmitter *ORE) {
1595+
using namespace ore;
15941596
ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadElim", LI)
1595-
<< "load of type " << ore::NV("Type", LI->getType())
1596-
<< " eliminated");
1597+
<< "load of type " << NV("Type", LI->getType()) << " eliminated"
1598+
<< setExtraArgs() << " in favor of "
1599+
<< NV("InfavorOfValue", AvailableValue));
15971600
}
15981601

15991602
/// Attempt to eliminate a load whose dependencies are
@@ -1666,7 +1669,7 @@ bool GVN::processNonLocalLoad(LoadInst *LI) {
16661669
MD->invalidateCachedPointerInfo(V);
16671670
markInstructionForDeletion(LI);
16681671
++NumGVNLoad;
1669-
reportLoadElim(LI, ORE);
1672+
reportLoadElim(LI, V, ORE);
16701673
return true;
16711674
}
16721675

@@ -1813,7 +1816,7 @@ bool GVN::processLoad(LoadInst *L) {
18131816
patchAndReplaceAllUsesWith(L, AvailableValue);
18141817
markInstructionForDeletion(L);
18151818
++NumGVNLoad;
1816-
reportLoadElim(L, ORE);
1819+
reportLoadElim(L, AvailableValue, ORE);
18171820
// Tell MDA to rexamine the reused pointer since we might have more
18181821
// information after forwarding it.
18191822
if (MD && AvailableValue->getType()->getScalarType()->isPointerTy())

test/Transforms/GVN/opt-remarks.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
; YAML-NEXT: - String: 'load of type '
1616
; YAML-NEXT: - Type: i32
1717
; YAML-NEXT: - String: ' eliminated'
18+
; YAML-NEXT: - String: ' in favor of '
19+
; YAML-NEXT: - InfavorOfValue: i
1820
; YAML-NEXT: ...
1921
; YAML-NEXT: --- !Passed
2022
; YAML-NEXT: Pass: gvn
@@ -24,6 +26,8 @@
2426
; YAML-NEXT: - String: 'load of type '
2527
; YAML-NEXT: - Type: i32
2628
; YAML-NEXT: - String: ' eliminated'
29+
; YAML-NEXT: - String: ' in favor of '
30+
; YAML-NEXT: - InfavorOfValue: '4'
2731
; YAML-NEXT: ...
2832
; YAML-NEXT: --- !Passed
2933
; YAML-NEXT: Pass: gvn
@@ -33,6 +37,8 @@
3337
; YAML-NEXT: - String: 'load of type '
3438
; YAML-NEXT: - Type: i32
3539
; YAML-NEXT: - String: ' eliminated'
40+
; YAML-NEXT: - String: ' in favor of '
41+
; YAML-NEXT: - InfavorOfValue: load
3642
; YAML-NEXT: ...
3743

3844

0 commit comments

Comments
 (0)