Skip to content

Commit 7a521e5

Browse files
committed
[SandboxVec] Optimization remarks
This patch implements the emition of optimization remarks when some region gets successfully vectorized. For now the debug location used is Rgn.begin(), which is not ideal.
1 parent 32dffdc commit 7a521e5

File tree

8 files changed

+60
-4
lines changed

8 files changed

+60
-4
lines changed

llvm/include/llvm/SandboxIR/Pass.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace llvm {
1717
class AAResults;
1818
class ScalarEvolution;
1919
class TargetTransformInfo;
20+
class OptimizationRemarkEmitter;
2021

2122
namespace sandboxir {
2223

@@ -27,17 +28,20 @@ class Analyses {
2728
AAResults *AA = nullptr;
2829
ScalarEvolution *SE = nullptr;
2930
TargetTransformInfo *TTI = nullptr;
31+
OptimizationRemarkEmitter *ORE = nullptr;
3032

3133
Analyses() = default;
3234

3335
public:
34-
Analyses(AAResults &AA, ScalarEvolution &SE, TargetTransformInfo &TTI)
35-
: AA(&AA), SE(&SE), TTI(&TTI) {}
36+
Analyses(AAResults &AA, ScalarEvolution &SE, TargetTransformInfo &TTI,
37+
OptimizationRemarkEmitter &ORE)
38+
: AA(&AA), SE(&SE), TTI(&TTI), ORE(&ORE) {}
3639

3740
public:
3841
AAResults &getAA() const { return *AA; }
3942
ScalarEvolution &getScalarEvolution() const { return *SE; }
4043
TargetTransformInfo &getTTI() const { return *TTI; }
44+
OptimizationRemarkEmitter &getORE() const { return *ORE; }
4145
/// For use by unit tests.
4246
static Analyses emptyForTesting() { return Analyses(); }
4347
};

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/MemoryLocation.h"
1818
#include "llvm/Analysis/ScalarEvolution.h"
1919
#include "llvm/Analysis/ValueTracking.h"
20+
#include "llvm/IR/DiagnosticInfo.h"
2021
#include "llvm/IR/Verifier.h"
2122
#include "llvm/SandboxIR/Function.h"
2223
#include "llvm/SandboxIR/Instruction.h"
@@ -131,6 +132,15 @@ class Utils {
131132
const auto &LLVMF = *cast<llvm::Function>(F->Val);
132133
return llvm::verifyFunction(LLVMF, &OS);
133134
}
135+
136+
/// Returns an optimization remark for \p I's location.
137+
// TODO: OptimizationRemark can leak llvm IR through getRegion().
138+
static OptimizationRemark getOptimizationRemark(const char *PassName,
139+
StringRef RemarkName,
140+
Instruction *I) {
141+
return OptimizationRemark(PassName, RemarkName,
142+
cast<llvm::Instruction>(I->Val));
143+
}
134144
};
135145

136146
} // namespace llvm::sandboxir

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
#include "llvm/Support/Debug.h"
1717

18-
#define DEBUG_TYPE "sandbox-vectorizer"
18+
#define PASS_NAME "sandbox-vectorizer"
19+
#define DEBUG_TYPE PASS_NAME
1920
#define DEBUG_PREFIX "SBVec: "
2021

2122
#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_DEBUG_H

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212

1313
#include "llvm/Analysis/AliasAnalysis.h"
14+
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
1415
#include "llvm/Analysis/ScalarEvolution.h"
1516
#include "llvm/IR/PassManager.h"
1617
#include "llvm/SandboxIR/Context.h"
@@ -24,6 +25,7 @@ class SandboxVectorizerPass : public PassInfoMixin<SandboxVectorizerPass> {
2425
TargetTransformInfo *TTI = nullptr;
2526
AAResults *AA = nullptr;
2627
ScalarEvolution *SE = nullptr;
28+
OptimizationRemarkEmitter *ORE = nullptr;
2729
// NOTE: We define the Context as a pass-scope object instead of local object
2830
// in runOnFunction() because the passes defined in the pass-manager need
2931
// access to it for registering/deregistering callbacks during construction

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h"
10+
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
11+
#include "llvm/SandboxIR/Utils.h"
1012
#include "llvm/Support/CommandLine.h"
1113
#include "llvm/Support/InstructionCost.h"
1214
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Debug.h"
@@ -32,6 +34,10 @@ bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) {
3234
if (CostAfterMinusBefore < -CostThreshold) {
3335
bool HasChanges = !Tracker.empty();
3436
Tracker.accept();
37+
// TODO: Use a more accurate debug location than Rgn.begin().
38+
A.getORE().emit(
39+
Utils::getOptimizationRemark(PASS_NAME, "Vectorized", *Rgn.begin())
40+
<< "Vectorized with cost " << ore::NV("Cost", CostAfterMinusBefore));
3541
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "*** Transaction Accept ***\n");
3642
return HasChanges;
3743
}

llvm/lib/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ PreservedAnalyses SandboxVectorizerPass::run(Function &F,
7272
TTI = &AM.getResult<TargetIRAnalysis>(F);
7373
AA = &AM.getResult<AAManager>(F);
7474
SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
75+
ORE = &AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
7576

7677
bool Changed = runImpl(F);
7778
if (!Changed)
@@ -132,7 +133,7 @@ bool SandboxVectorizerPass::runImpl(Function &LLVMF) {
132133

133134
// Create SandboxIR for LLVMF and run BottomUpVec on it.
134135
sandboxir::Function &F = *Ctx->createFunction(&LLVMF);
135-
sandboxir::Analyses A(*AA, *SE, *TTI);
136+
sandboxir::Analyses A(*AA, *SE, *TTI, *ORE);
136137
bool Change = FPM.runOnFunction(F, A);
137138
// Given that sandboxir::Context `Ctx` is defined at a pass-level scope, the
138139
// maps from LLVM IR to Sandbox IR may go stale as later passes remove LLVM IR
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=sandbox-vectorizer -sbvec-vec-reg-bits=1024 -pass-remarks-output=%t < %s | FileCheck %s
3+
; RUN: FileCheck --input-file=%t --check-prefix=REMARKS %s
4+
5+
; REMARKS-LABEL: --- !Passed
6+
; REMARKS-LABEL: Pass: sandbox-vectorizer
7+
; REMARKS-LABEL: Name: Vectorized
8+
; REMARKS-LABEL: Function: remarks1
9+
; REMARKS-LABEL: Args:
10+
; REMARKS-LABEL: - String: 'Vectorized with cost '
11+
; REMARKS-LABEL: - Cost: '-3'
12+
13+
define void @remarks1(ptr %ptr) {
14+
; CHECK-LABEL: define void @remarks1(
15+
; CHECK-SAME: ptr [[PTR:%.*]]) {
16+
; CHECK-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
17+
; CHECK-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4, !sandboxvec [[META0:![0-9]+]]
18+
; CHECK-NEXT: store <2 x float> [[VECL]], ptr [[PTR0]], align 4, !sandboxvec [[META0]]
19+
; CHECK-NEXT: ret void
20+
;
21+
%ptr0 = getelementptr float, ptr %ptr, i32 0
22+
%ptr1 = getelementptr float, ptr %ptr, i32 1
23+
%ld0 = load float, ptr %ptr0
24+
%ld1 = load float, ptr %ptr1
25+
store float %ld0, ptr %ptr0
26+
store float %ld1, ptr %ptr1
27+
ret void
28+
}
29+
;.
30+
; CHECK: [[META0]] = distinct !{!"sandboxregion"}
31+
;.

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ define void @foo() {
5757
AM.registerPass([] { return AssumptionAnalysis(); });
5858
AM.registerPass([] { return DominatorTreeAnalysis(); });
5959
AM.registerPass([] { return LoopAnalysis(); });
60+
AM.registerPass([] { return OptimizationRemarkEmitterAnalysis(); });
6061
SVecPass.run(LLVMF, AM);
6162
// This shouldn't crash.
6263
SVecPass.run(LLVMF, AM);

0 commit comments

Comments
 (0)