Skip to content

Commit 16a4f20

Browse files
committed
Let Clang handle compiler optimizations
We no longer need to do anything different from the default. Fixes #189.
1 parent 4d0f07c commit 16a4f20

File tree

1 file changed

+13
-40
lines changed

1 file changed

+13
-40
lines changed

src/core/Program.cpp

+13-40
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,9 @@ bool Program::build(const char *options, list<Header> headers)
282282
}
283283
args.push_back(cl_ext.c_str());
284284

285-
// Disable Clang's optimizations.
286-
// We will manually run optimization passes and legalize the IR later.
287-
args.push_back("-O0");
288-
289-
bool optimize = true;
285+
bool defaultOptimization = true;
290286
const char *clstd = NULL;
291287

292-
// Disable optimizations by default if in interactive mode
293-
if (checkEnv("OCLGRIND_INTERACTIVE"))
294-
optimize = false;
295-
296288
// Add OpenCL build options
297289
const char *mainOptions = options;
298290
const char *extraOptions = getenv("OCLGRIND_BUILD_OPTIONS");
@@ -320,15 +312,9 @@ bool Program::build(const char *options, list<Header> headers)
320312
strcmp(opt, "-cl-unsafe-math-optimizations") != 0)
321313
{
322314
// Check for optimization flags
323-
if (strcmp(opt, "-O0") == 0 || strcmp(opt, "-cl-opt-disable") == 0)
315+
if (strncmp(opt, "-O", 2) == 0 || strcmp(opt, "-cl-opt-disable") == 0)
324316
{
325-
optimize = false;
326-
continue;
327-
}
328-
else if (strncmp(opt, "-O", 2) == 0)
329-
{
330-
optimize = true;
331-
continue;
317+
defaultOptimization = false;
332318
}
333319

334320
// Clang no longer supports -cl-no-signed-zeros
@@ -346,6 +332,16 @@ bool Program::build(const char *options, list<Header> headers)
346332
}
347333
}
348334

335+
if (defaultOptimization)
336+
{
337+
// Disable optimizations by default if in interactive mode
338+
if (checkEnv("OCLGRIND_INTERACTIVE"))
339+
args.push_back("-O0");
340+
// Otherwise, default to optimizing for size
341+
else
342+
args.push_back("-Oz");
343+
}
344+
349345
if (!clstd)
350346
{
351347
clstd = "-cl-std=CL1.2";
@@ -495,29 +491,6 @@ bool Program::build(const char *options, list<Header> headers)
495491
stripDebugIntrinsics();
496492
}
497493

498-
// Run optimizations on module
499-
if (optimize)
500-
{
501-
// Initialize pass managers
502-
llvm::legacy::PassManager modulePasses;
503-
llvm::legacy::FunctionPassManager functionPasses(m_module.get());
504-
505-
// Populate pass managers with -Oz
506-
llvm::PassManagerBuilder builder;
507-
builder.OptLevel = 2;
508-
builder.SizeLevel = 2;
509-
builder.populateModulePassManager(modulePasses);
510-
builder.populateFunctionPassManager(functionPasses);
511-
512-
// Run passes
513-
functionPasses.doInitialization();
514-
llvm::Module::iterator fItr;
515-
for (fItr = m_module->begin(); fItr != m_module->end(); fItr++)
516-
functionPasses.run(*fItr);
517-
functionPasses.doFinalization();
518-
modulePasses.run(*m_module);
519-
}
520-
521494
removeLValueLoads();
522495

523496
allocateProgramScopeVars();

0 commit comments

Comments
 (0)