From d351623df06fcaacac5fd9d833b1f3ad2a7fbc34 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Fri, 13 Dec 2024 13:11:55 -0800 Subject: [PATCH] Fix StringOptimization to handle load_borrow rdar://140229560 --- .../Transforms/StringOptimization.cpp | 15 +++++++++------ test/SILOptimizer/string_optimization.swift | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/SILOptimizer/Transforms/StringOptimization.cpp b/lib/SILOptimizer/Transforms/StringOptimization.cpp index d4e2c0fb8c99b..e4457191514d3 100644 --- a/lib/SILOptimizer/Transforms/StringOptimization.cpp +++ b/lib/SILOptimizer/Transforms/StringOptimization.cpp @@ -441,6 +441,8 @@ isStringStoreToIdentifyableObject(SILInstruction *inst) { case SILInstructionKind::DeallocStackInst: case SILInstructionKind::LoadInst: break; + case SILInstructionKind::LoadBorrowInst: + break; case SILInstructionKind::DebugValueInst: if (DebugValueInst::hasAddrVal(user)) break; @@ -579,12 +581,13 @@ StringOptimization::getStringFromStaticLet(SILValue value) { // %ptr_to_global = apply %addressor() // %global_addr = pointer_to_address %ptr_to_global // %value = load %global_addr - auto *load = dyn_cast(value); - if (!load) - return StringInfo::unknown(); + if (!isa(value) && !isa(value)) { + return StringInfo::unknown(); + } + auto *load = value->getDefiningInstruction(); SILFunction *initializer = nullptr; - auto *globalAddr = dyn_cast(load->getOperand()); + auto *globalAddr = dyn_cast(load->getOperand(0)); if (globalAddr) { // The global accessor is inlined. @@ -600,7 +603,7 @@ StringOptimization::getStringFromStaticLet(SILValue value) { } else { // The global accessor is not inlined, yet. - auto *pta = dyn_cast(load->getOperand()); + auto *pta = dyn_cast(load->getOperand(0)); if (!pta) return StringInfo::unknown(); @@ -651,7 +654,7 @@ StringOptimization::getStringFromStaticLet(SILValue value) { // This check is probably not needed, but let's be on the safe side: // it prevents an infinite recursion if the initializer of the global is // itself a load of another global, and so on. - if (isa(initVal)) + if (isa(initVal) || isa(initVal)) return StringInfo::unknown(); return getStringInfo(initVal); diff --git a/test/SILOptimizer/string_optimization.swift b/test/SILOptimizer/string_optimization.swift index 9a2ce4a8cf791..e20a2370e9c40 100644 --- a/test/SILOptimizer/string_optimization.swift +++ b/test/SILOptimizer/string_optimization.swift @@ -1,4 +1,5 @@ // RUN: %target-build-swift -O %s -module-name=test -Xfrontend -sil-verify-all -emit-sil | %FileCheck %s +// RUN: %target-build-swift -O %s -module-name=test -Xfrontend -sil-verify-all -emit-sil -Xfrontend -enable-ossa-modules | %FileCheck %s // RUN: %empty-directory(%t) // RUN: %target-build-swift -O -module-name=test %s -o %t/a.out