Skip to content

Commit 705faee

Browse files
authored
Coverage-based estimation of loop weights (#16)
1 parent 0c0689a commit 705faee

File tree

10 files changed

+513
-2
lines changed

10 files changed

+513
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//=== CountedRegionsExtractor.h - Extracting Of CountedRegions From Code Coverage (Clang) --*- C++ -*===//
2+
//
3+
// Traits Static Analyzer (SAPFOR)
4+
//
5+
// Copyright 2018 DVM System Group
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===---------------------------------------------------------------------===//
20+
//
21+
// This file declares a pass to extract & store
22+
// all the CountedRegions along with their ExecutionCounts
23+
// from a given code coverage file.
24+
//
25+
//===---------------------------------------------------------------------===//
26+
27+
#ifndef TSAR_CLANG_COUNTED_REGIONS_EXTRACTOR_H
28+
#define TSAR_CLANG_COUNTED_REGIONS_EXTRACTOR_H
29+
30+
#include <bcl/utility.h>
31+
#include "tsar/Transform/Clang/Passes.h"
32+
#include <llvm/Pass.h>
33+
#include <llvm/ProfileData/Coverage/CoverageMapping.h>
34+
#include <vector>
35+
36+
namespace llvm {
37+
/// This immutable pass extracts & stores CountedRegions
38+
/// from a given code coverage file
39+
class ClangCountedRegionsExtractor : public ImmutablePass, private bcl::Uncopyable {
40+
public:
41+
static char ID;
42+
43+
ClangCountedRegionsExtractor();
44+
45+
void initializePass() override;
46+
const std::vector<coverage::CountedRegion> &getCountedRegions() const noexcept;
47+
void getAnalysisUsage(AnalysisUsage &AU) const override;
48+
49+
private:
50+
std::vector<coverage::CountedRegion> CountedRegions{};
51+
};
52+
}
53+
#endif//TSAR_CLANG_COUNTED_REGIONS_EXTRACTOR_H

include/tsar/Analysis/Clang/Passes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace llvm {
3232
class PassRegistry;
3333
class FunctionPass;
3434
class ModulePass;
35+
class ImmutablePass;
3536

3637
/// Initialize all passes to perform source-base analysis of C programs.
3738
void initializeClangAnalysis(PassRegistry &Registry);
@@ -149,5 +150,13 @@ void initializeClangIncludeTreeOnlyViewerPass(PassRegistry &Registry);
149150
/// Create a pass to display source file tree (filenames instead of
150151
/// full paths).
151152
ModulePass *createClangIncludeTreeOnlyViewer();
153+
154+
/// Initialize a pass to extract & store all the CountedRegions
155+
/// from a given code coverage file.
156+
void initializeClangCountedRegionsExtractorPass(PassRegistry &Registry);
157+
158+
/// Create a pass to extract & store all the CountedRegions
159+
/// from a given code coverage file.
160+
ImmutablePass *createClangCountedRegionsExtractor();
152161
}
153162
#endif//TSAR_CLANG_ANALYSIS_PASSES_H
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//=== LoopWeightsEstimator.h - Loop Weights Estimator (Clang) --*- C++ -*===//
2+
//
3+
// Traits Static Analyzer (SAPFOR)
4+
//
5+
// Copyright 2018 DVM System Group
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===---------------------------------------------------------------------===//
20+
//
21+
// This file declares a pass to estimate loop weights in a source code.
22+
//
23+
//===---------------------------------------------------------------------===//
24+
25+
#ifndef TSAR_CLANG_LOOP_WEIGHTS_H
26+
#define TSAR_CLANG_LOOP_WEIGHTS_H
27+
28+
29+
#include <bcl/utility.h>
30+
#include "tsar/Transform/Clang/Passes.h"
31+
#include <llvm/IR/Metadata.h>
32+
#include <llvm/Pass.h>
33+
34+
#include <map>
35+
36+
37+
namespace llvm {
38+
/// This per-module pass performs estimation of loop weights
39+
class ClangLoopWeightsEstimator : public ModulePass, private bcl::Uncopyable {
40+
public:
41+
static char ID;
42+
43+
ClangLoopWeightsEstimator();
44+
45+
bool runOnModule(Module &M) override;
46+
const std::map<MDNode *, uint64_t> &getLoopWeights() const noexcept;
47+
void getAnalysisUsage(AnalysisUsage &AU) const override;
48+
49+
private:
50+
std::map<Function *, uint64_t> mFunctionEntryCounts{};
51+
std::map<Function *, uint64_t> mFunctionBaseWeights{};
52+
std::map<Function *, uint64_t> mFunctionEffectiveWeights{};
53+
std::map<MDNode *, uint64_t> mLoopBaseWeights{};
54+
std::map<MDNode *, uint64_t> mLoopEffectiveWeights{};
55+
};
56+
}
57+
#endif//TSAR_CLANG_LOOP_WEIGHTS_H

include/tsar/Transform/Clang/Passes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,11 @@ void initializeClangLoopReversePass(PassRegistry &Registry);
114114

115115
/// Create a pass to reverse loop.
116116
ModulePass *createClangLoopReverse();
117+
118+
/// Initialize a pass to estimate loop weights in a source code.
119+
void initializeClangLoopWeightsEstimatorPass(PassRegistry &Registry);
120+
121+
/// Create a pass to estimate loop weights in a source code.
122+
ModulePass *createClangLoopWeightsEstimator();
117123
}
118124
#endif//TSAR_CLANG_TRANSFORM_PASSES_H

lib/Analysis/Clang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ set(ANALYSIS_SOURCES Passes.cpp DIMemoryMatcher.cpp NoMacroAssert.cpp
22
MemoryMatcher.cpp LoopMatcher.cpp ExpressionMatcher.cpp CanonicalLoop.cpp
33
PerfectLoop.cpp GlobalInfoExtractor.cpp ControlFlowTraits.cpp
44
RegionDirectiveInfo.cpp VariableCollector.cpp ASTDependenceAnalysis.cpp
5-
IncludeTree.cpp Utils.cpp)
5+
IncludeTree.cpp Utils.cpp CountedRegionsExtractor.cpp)
66

77

88
if(MSVC_IDE)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//=== CountedRegionsExtractor.cpp - Extracting Of CountedRegions From Code Coverage (Clang) --*- C++ -*===//
2+
//
3+
// Traits Static Analyzer (SAPFOR)
4+
//
5+
// Copyright 2018 DVM System Group
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===---------------------------------------------------------------------===//
20+
//
21+
// This file defines a pass to extract & store
22+
// all the CountedRegions along with their ExecutionCounts
23+
// from a given code coverage file.
24+
//
25+
//===---------------------------------------------------------------------===//
26+
27+
#include "tsar/Analysis/Clang/CountedRegionsExtractor.h"
28+
#include "tsar/Analysis/Clang/GlobalInfoExtractor.h"
29+
#include "tsar/Analysis/Clang/NoMacroAssert.h"
30+
#include "tsar/Core/Query.h"
31+
#include "tsar/Frontend/Clang/TransformationContext.h"
32+
#include <clang/AST/Decl.h>
33+
#include <clang/AST/RecursiveASTVisitor.h>
34+
#include <clang/AST/Stmt.h>
35+
#include <clang/Basic/SourceLocation.h>
36+
#include <llvm/ADT/StringRef.h>
37+
#include <llvm/IR/Function.h>
38+
#include <llvm/IR/Module.h>
39+
#include <llvm/ProfileData/Coverage/CoverageMapping.h>
40+
#include <llvm/Support/Debug.h>
41+
#include <llvm/Support/raw_ostream.h>
42+
#include <vector>
43+
44+
#include <iostream>
45+
46+
using namespace clang;
47+
using namespace llvm;
48+
using namespace tsar;
49+
50+
#undef DEBUG_TYPE
51+
#define DEBUG_TYPE "clang-counted-regions-extractor"
52+
53+
char ClangCountedRegionsExtractor::ID = 0;
54+
55+
INITIALIZE_PASS_IN_GROUP(ClangCountedRegionsExtractor, "clang-counted-regions-extractor",
56+
"Extracting Of CountedRegions From Code Coverage (Clang)", false, false,
57+
TransformationQueryManager::getPassRegistry())
58+
59+
ClangCountedRegionsExtractor::ClangCountedRegionsExtractor() : ImmutablePass(ID) {
60+
initializeClangCountedRegionsExtractorPass(*PassRegistry::getPassRegistry());
61+
}
62+
63+
// Extracts & stores CountedRegions from code coverage
64+
void ClangCountedRegionsExtractor::initializePass() {
65+
std::string ObjectFilename{ "/home/alex000/Documents/sapfor-total/working/main" };
66+
std::string ProfileFilename{ "/home/alex000/Documents/sapfor-total/working/main.profdata" };
67+
68+
StringRef ObjectFilenameRef{ ObjectFilename };
69+
ArrayRef<StringRef> ObjectFilenameRefs{ ObjectFilenameRef };
70+
StringRef ProfileFilenameRef{ ProfileFilename };
71+
72+
auto CoverageOrErr = coverage::CoverageMapping::load(ObjectFilenameRefs, ProfileFilenameRef);
73+
if (!CoverageOrErr) {
74+
errs() << "COVERAGE ERROR\n";
75+
return;
76+
}
77+
78+
LLVM_DEBUG(llvm::dbgs() << "Coverage loading done\n";);
79+
80+
const auto &Coverage = CoverageOrErr->get();
81+
82+
for (auto &FRI : Coverage->getCoveredFunctions()) {
83+
for (auto CR : FRI.CountedRegions) {
84+
CountedRegions.push_back(CR);
85+
}
86+
}
87+
88+
LLVM_DEBUG(llvm::dbgs() << "Stored CountedRegions:\n";);
89+
for (const auto &CR : CountedRegions) {
90+
LLVM_DEBUG(
91+
llvm::dbgs() << "\tstart: " << CR.LineStart << ":" << CR.ColumnStart
92+
<< ", end: " << CR.LineEnd << ":" << CR.ColumnEnd
93+
<< ", count: " << CR.ExecutionCount << "\n";
94+
);
95+
}
96+
LLVM_DEBUG(llvm::dbgs() << "\n";);
97+
}
98+
99+
const std::vector<coverage::CountedRegion> &
100+
ClangCountedRegionsExtractor::getCountedRegions() const noexcept {
101+
return CountedRegions;
102+
}
103+
104+
void ClangCountedRegionsExtractor::getAnalysisUsage(AnalysisUsage &AU) const {
105+
AU.setPreservesAll();
106+
}
107+
108+
ImmutablePass *llvm::createClangCountedRegionsExtractor() {
109+
return new ClangCountedRegionsExtractor();
110+
}

lib/Analysis/Clang/Passes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void llvm::initializeClangAnalysis(PassRegistry &Registry) {
4242
initializeClangIncludeTreeOnlyPrinterPass(Registry);
4343
initializeClangIncludeTreeViewerPass(Registry);
4444
initializeClangIncludeTreeOnlyViewerPass(Registry);
45+
initializeClangCountedRegionsExtractorPass(Registry);
4546
// Initialize checkers.
4647
initializeClangNoMacroAssertPass(Registry);
4748
}

lib/Transform/Clang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ set(TRANSFORM_SOURCES Passes.cpp ExprPropagation.cpp Inline.cpp RenameLocal.cpp
22
DeadDeclsElimination.cpp Format.cpp OpenMPAutoPar.cpp DVMHWriter.cpp
33
SharedMemoryAutoPar.cpp DVMHDirecitves.cpp DVMHSMAutoPar.cpp
44
DVMHDataTransferIPO.cpp StructureReplacement.cpp LoopInterchange.cpp
5-
LoopReversal.cpp)
5+
LoopReversal.cpp LoopWeightsEstimator.cpp)
66

77
if(MSVC_IDE)
88
file(GLOB_RECURSE TRANSFORM_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}

0 commit comments

Comments
 (0)