Skip to content

Commit 5621ab3

Browse files
authored
Merge pull request #67949 from meg-gupta/fixoptnone
Generate appropriate LLVM IR attributes for custom optimization modes set on a SILFunction
2 parents d7be399 + 6725657 commit 5621ab3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,21 @@ IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
19021902
if (f->getLoweredFunctionType()->isCoroutine()) {
19031903
CurFn->addFnAttr(llvm::Attribute::NoInline);
19041904
}
1905+
1906+
auto optMode = f->getOptimizationMode();
1907+
if (optMode != OptimizationMode::NotSet &&
1908+
optMode != f->getModule().getOptions().OptMode) {
1909+
if (optMode == OptimizationMode::NoOptimization) {
1910+
CurFn->addFnAttr(llvm::Attribute::OptimizeNone);
1911+
// LLVM requires noinline attribute along with optnone
1912+
CurFn->addFnAttr(llvm::Attribute::NoInline);
1913+
}
1914+
if (optMode == OptimizationMode::ForSize) {
1915+
CurFn->addFnAttr(llvm::Attribute::OptimizeForSize);
1916+
}
1917+
// LLVM doesn't have an attribute for -O
1918+
}
1919+
19051920
// Emit the thunk that calls the previous implementation if this is a dynamic
19061921
// replacement.
19071922
if (f->getDynamicallyReplacedFunction()) {

test/IRGen/optmode.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swiftc_driver %s -emit-ir -O | %FileCheck %s
2+
3+
// CHECK: @"$s7optmode7square11nS2i_tF"{{.*}}[[ATTR1:#[0-9]+]]
4+
@_optimize(none)
5+
func square1(n: Int) -> Int {
6+
return n * n
7+
}
8+
9+
// CHECK: @"$s7optmode7square21nS2i_tF"{{.*}}[[ATTR2:#[0-9]+]]
10+
@_optimize(size)
11+
func square2(n: Int) -> Int {
12+
return n * n
13+
}
14+
15+
// CHECK: attributes [[ATTR1]] = { {{.*}}noinline optnone{{.*}} }
16+
// CHECK: attributes [[ATTR2]] = { {{.*}}optsize{{.*}} }

0 commit comments

Comments
 (0)