Index: lib/CodeGen/CGObjCGNU.cpp =================================================================== --- lib/CodeGen/CGObjCGNU.cpp +++ lib/CodeGen/CGObjCGNU.cpp @@ -3541,12 +3541,16 @@ ConstantInitBuilder builder(CGM); auto selectors = builder.beginArray(selStructTy); auto &table = SelectorTable; // MSVC workaround - for (auto &entry : table) { + std::vector allSelectors; + for (auto &entry : table) + allSelectors.push_back(entry.first); + llvm::sort(allSelectors.begin(), allSelectors.end()); - std::string selNameStr = entry.first.getAsString(); + for (auto &untypedSel : allSelectors) { + std::string selNameStr = untypedSel.getAsString(); llvm::Constant *selName = ExportUniqueString(selNameStr, ".objc_sel_name"); - for (TypedSelector &sel : entry.second) { + for (TypedSelector &sel : table[untypedSel]) { llvm::Constant *selectorTypeEncoding = NULLPtr; if (!sel.first.empty()) selectorTypeEncoding = Index: test/CodeGenObjC/gnu-deterministic-selectors.m =================================================================== --- test/CodeGenObjC/gnu-deterministic-selectors.m +++ test/CodeGenObjC/gnu-deterministic-selectors.m @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-freebsd -fobjc-runtime=gnustep-1.5 %s -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-unknown-freebsd -fobjc-runtime=gcc %s -emit-llvm -o - | FileCheck %s + +// Check that these selectors are emitted in alphabetical order. +// The order doesn't actually matter, only that it doesn't vary across runs. +// Clang sorts them when targeting a GCC-like ABI to guarantee determinism. +// CHECK: @.objc_selector_list = internal global [6 x { i8*, i8* }] [{ i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.objc_sel_namea, i64 0, i64 0), i8* null }, { i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.objc_sel_nameg, i64 0, i64 0), i8* null }, { i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.objc_sel_namej, i64 0, i64 0), i8* null }, { i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.objc_sel_namel, i64 0, i64 0), i8* null }, { i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.objc_sel_namez, i64 0, i64 0), i8* null }, { i8*, i8* } zeroinitializer], align 8 + + +void f() { + SEL a = @selector(z); + SEL b = @selector(a); + SEL c = @selector(g); + SEL d = @selector(l); + SEL e = @selector(j); +}