Skip to content

Commit fdcd506

Browse files
committed
IRGen: add a function suitable for emitting linker directive as global variable
1 parent c484894 commit fdcd506

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,49 @@ llvm::GlobalVariable *swift::irgen::createVariable(
19291929
return var;
19301930
}
19311931

1932+
llvm::GlobalVariable *
1933+
swift::irgen::createLinkerDirectiveVariable(IRGenModule &IGM, StringRef name) {
1934+
1935+
// A prefix of \1 can avoid further mangling of the symbol (prefixing _).
1936+
llvm::SmallString<32> NameWithFlag;
1937+
NameWithFlag.push_back('\1');
1938+
NameWithFlag.append(name);
1939+
name = NameWithFlag.str();
1940+
static const uint8_t Size = 8;
1941+
static const uint8_t Alignment = 8;
1942+
1943+
// Use a char type as the type for this linker directive.
1944+
auto ProperlySizedIntTy = SILType::getBuiltinIntegerType(
1945+
Size, IGM.getSwiftModule()->getASTContext());
1946+
auto storageType = IGM.getStorageType(ProperlySizedIntTy);
1947+
1948+
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
1949+
if (existingValue) {
1950+
auto existingVar = dyn_cast<llvm::GlobalVariable>(existingValue);
1951+
if (existingVar && isPointerTo(existingVar->getType(), storageType))
1952+
return existingVar;
1953+
1954+
IGM.error(SourceLoc(),
1955+
"program too clever: variable collides with existing symbol " +
1956+
name);
1957+
1958+
// Note that this will implicitly unique if the .unique name is also taken.
1959+
existingValue->setName(name + ".unique");
1960+
}
1961+
1962+
llvm::GlobalValue::LinkageTypes Linkage =
1963+
llvm::GlobalValue::LinkageTypes::ExternalLinkage;
1964+
auto var = new llvm::GlobalVariable(IGM.Module, storageType, /*constant*/true,
1965+
Linkage, /*Init to zero*/llvm::Constant::getNullValue(storageType), name);
1966+
ApplyIRLinkage({Linkage,
1967+
llvm::GlobalValue::VisibilityTypes::DefaultVisibility,
1968+
llvm::GlobalValue::DLLStorageClassTypes::DefaultStorageClass}).to(var);
1969+
var->setAlignment(Alignment);
1970+
disableAddressSanitizer(IGM, var);
1971+
IGM.addUsedGlobal(var);
1972+
return var;
1973+
}
1974+
19321975
void swift::irgen::disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariable *var) {
19331976
// Add an operand to llvm.asan.globals blacklisting this global variable.
19341977
llvm::Metadata *metadata[] = {

lib/IRGen/GenDecl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ namespace irgen {
5252
Optional<SILLocation> DebugLoc = None,
5353
StringRef DebugName = StringRef(), bool heapAllocated = false);
5454

55+
llvm::GlobalVariable *
56+
createLinkerDirectiveVariable(IRGenModule &IGM, StringRef Name);
57+
5558
void disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariable *var);
5659
}
5760
}

test/attr/Inputs/SymbolMove/LowLevel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
@available(OSX 10.8, *)
12
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
23
public func printMessageMoved() {
34
print("Hello from LowLevel")
45
}
56

7+
@available(OSX 10.8, *)
68
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
79
public struct Entity {
810
public let value = "LowLevel"
@@ -11,6 +13,7 @@ public struct Entity {
1113
}
1214

1315
// =================== Move protocol =================================//
16+
@available(OSX 10.8, *)
1417
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
1518
public protocol Box {
1619
associatedtype Item
@@ -19,18 +22,21 @@ public protocol Box {
1922
func shape() -> String
2023
}
2124

25+
@available(OSX 10.8, *)
2226
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
2327
extension Box {
2428
public func shape() -> String { return "round"}
2529
}
2630

31+
@available(OSX 10.8, *)
2732
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
2833
public struct Candy {
2934
public var kind = "candy"
3035
public init() {}
3136
}
3237

3338
// =================== Move enum ============================ //
39+
@available(OSX 10.8, *)
3440
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
3541
public enum LanguageKind: Int {
3642
case Cpp = 1
@@ -39,6 +45,7 @@ public enum LanguageKind: Int {
3945
}
4046

4147
// =================== Move class ============================ //
48+
@available(OSX 10.8, *)
4249
@_originallyDefinedIn(module: "HighLevel", OSX 10.10)
4350
open class Vehicle {
4451
public init() {}

0 commit comments

Comments
 (0)