Skip to content

Commit 004eb02

Browse files
committed
PGO fixes
1 parent a02ad76 commit 004eb02

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
2626
find_program(XXD_BIN xxd)
2727

2828
# TODO should depend on OS
29-
set(POLYGEIST_PGO_DATA_DIR "/var/tmp/polygeist/pgo/" CACHE STRING "Directory for PGO data")
29+
set(POLYGEIST_PGO_DEFAULT_DATA_DIR "/var/tmp/polygeist/pgo/" CACHE STRING "Directory for PGO data")
30+
set(POLYGEIST_PGO_ALTERNATIVE_ENV_VAR "POLYGEIST_PGO_ALTERNATIVE" CACHE STRING "Env var name to specify alternative to profile")
31+
set(POLYGEIST_PGO_DATA_DIR_ENV_VAR "POLYGEIST_PGO_DATA_DIR" CACHE STRING "Env var name to specify PGO data dir")
3032

3133
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3234
project(polygeist LANGUAGES CXX C)

lib/polygeist/ExecutionEngine/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ if(POLYGEIST_ENABLE_CUDA)
5252
${bc_flags}
5353
${infile} -o ${bc_outfile}
5454
-I${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
55-
-DPOLYGEIST_PGO_DATA_DIR="${POLYGEIST_PGO_DATA_DIR}"
55+
-DPOLYGEIST_PGO_DEFAULT_DATA_DIR="${POLYGEIST_PGO_DEFAULT_DATA_DIR}"
56+
-DPOLYGEIST_PGO_ALTERNATIVE_ENV_VAR="${POLYGEIST_PGO_ALTERNATIVE_ENV_VAR}"
57+
-DPOLYGEIST_PGO_DATA_DIR_ENV_VAR="${POLYGEIST_PGO_DATA_DIR_ENV_VAR}"
5658
DEPENDS ${infile}
5759
COMMENT "Building LLVM bitcode ${bc_outfile}"
5860
VERBATIM

lib/polygeist/ExecutionEngine/CudaRuntimeWrappers.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,21 @@ class PGOState {
8686
struct timespec start_clock;
8787
};
8888

89-
// TODO define these in cmake files (depending on target OS and used in the
90-
// compiler too)
91-
static constexpr const char *dirname = POLYGEIST_PGO_DATA_DIR;
92-
static constexpr const char *alternativeEnvVar =
93-
"POLYGEIST_PGO_KERNEL_ALTERNATIVE";
94-
9589
inline static int alternative;
90+
inline static std::string dirname;
9691
inline thread_local static std::mutex mutex;
9792
inline thread_local static std::map<std::string, State *> states;
9893

9994
std::string kernelId;
10095
int totalAlternatives;
10196

102-
PGOState(const char *kernelId, int totalAlternatives)
103-
: kernelId(kernelId), totalAlternatives(totalAlternatives) {}
97+
PGOState(const char *kernelId_c, int totalAlternatives)
98+
: totalAlternatives(totalAlternatives) {
99+
kernelId = kernelId_c;
100+
for (char &c : kernelId)
101+
if (c == '/')
102+
c = '+';
103+
}
104104
void end() {
105105
struct timespec end_clock;
106106
cudaDeviceSynchronize();
@@ -123,7 +123,7 @@ class PGOState {
123123
if (0 <= alternative && alternative < totalAlternatives) {
124124
// TODO error handling
125125
std::ofstream ofile;
126-
ofile.open(std::string(dirname) + kernelId,
126+
ofile.open(std::string(dirname) + "/" + kernelId,
127127
std::ios::out | std::ios::app);
128128
ofile << alternative << " " << elapsed << std::endl;
129129
ofile.close();
@@ -149,12 +149,18 @@ class PGOState {
149149

150150
int getAlternative() {
151151
static int init = [&] {
152-
if (char *i = getenv(alternativeEnvVar)) {
152+
if (char *i = getenv(POLYGEIST_PGO_ALTERNATIVE_ENV_VAR)) {
153153
this->alternative = atoi(i);
154154
} else {
155-
std::cerr << alternativeEnvVar << " not defined" << std::endl;
155+
std::cerr << POLYGEIST_PGO_ALTERNATIVE_ENV_VAR << " not defined"
156+
<< std::endl;
156157
exit(1);
157158
}
159+
if (char *d = getenv(POLYGEIST_PGO_DATA_DIR_ENV_VAR)) {
160+
this->dirname = d;
161+
} else {
162+
this->dirname = POLYGEIST_PGO_DEFAULT_DATA_DIR;
163+
}
158164
std::filesystem::create_directories(dirname);
159165
return 0;
160166
}();

lib/polygeist/Passes/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ add_mlir_dialect_library(MLIRPolygeistTransforms
5353

5454
target_compile_definitions(obj.MLIRPolygeistTransforms
5555
PRIVATE
56-
POLYGEIST_PGO_DATA_DIR="${POLYGEIST_PGO_DATA_DIR}"
56+
POLYGEIST_PGO_DEFAULT_DATA_DIR="${POLYGEIST_PGO_DEFAULT_DATA_DIR}"
57+
POLYGEIST_PGO_ALTERNATIVE_ENV_VAR="${POLYGEIST_PGO_ALTERNATIVE_ENV_VAR}"
58+
POLYGEIST_PGO_DATA_DIR_ENV_VAR="${POLYGEIST_PGO_DATA_DIR_ENV_VAR}"
5759
)
5860

5961
if(POLYGEIST_ENABLE_CUDA)

lib/polygeist/Passes/ConvertPolygeistToLLVM.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,9 @@ struct LowerGPUAlternativesOp
16921692
return stream.str();
16931693
}();
16941694
locStr += gao->getAttrOfType<StringAttr>("polygeist.altop.id").data();
1695+
for (char &c : locStr)
1696+
if (c == '/')
1697+
c = '+';
16951698

16961699
if (PolygeistAlternativesMode == PAM_PGO_Profile) {
16971700
rewriter.setInsertionPoint(gao);
@@ -1734,15 +1737,21 @@ struct LowerGPUAlternativesOp
17341737
rewriter.eraseOp(gao);
17351738
return success();
17361739
} else if (PolygeistAlternativesMode == PAM_PGO_Opt) {
1737-
static constexpr const char *dirname = POLYGEIST_PGO_DATA_DIR;
1740+
std::string dirname = []() {
1741+
if (char *d = getenv(POLYGEIST_PGO_DATA_DIR_ENV_VAR)) {
1742+
return std::string(d);
1743+
} else {
1744+
return std::string(POLYGEIST_PGO_DEFAULT_DATA_DIR);
1745+
}
1746+
}();
17381747
// TODO error handling
17391748
std::ifstream ifile;
17401749
int numAlternatives = gao->getNumRegions();
17411750
std::vector<std::vector<double>> timings;
17421751
for (int i = 0; i < numAlternatives; i++) {
17431752
timings.push_back({});
17441753
}
1745-
ifile.open(std::string(dirname) + locStr, std::ios::in);
1754+
ifile.open(std::string(dirname) + "/" + locStr, std::ios::in);
17461755
while (ifile) {
17471756
int alt;
17481757
double time;
@@ -1763,6 +1772,7 @@ struct LowerGPUAlternativesOp
17631772
avgs.push_back(std::numeric_limits<double>::infinity());
17641773
} else {
17651774
// TODO might get some round off errors here, maybe use a better alg
1775+
// or median
17661776
avgs.push_back(
17671777
std::accumulate(timings[i].begin(), timings[i].end(), 0.0f) /
17681778
timings[i].size());

0 commit comments

Comments
 (0)