From c9bcde4e9569d9aa6a4fedcde5ad87f89f322334 Mon Sep 17 00:00:00 2001 From: Nate Chandler Date: Wed, 29 Oct 2025 16:02:32 -0700 Subject: [PATCH 1/3] [NFC] IRGen: Lift this function from IGF to IGM. --- lib/IRGen/GenPointerAuth.cpp | 6 +++++- lib/IRGen/IRGenModule.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/IRGen/GenPointerAuth.cpp b/lib/IRGen/GenPointerAuth.cpp index 94b6b81be8b66..588f395edfe95 100644 --- a/lib/IRGen/GenPointerAuth.cpp +++ b/lib/IRGen/GenPointerAuth.cpp @@ -771,6 +771,10 @@ void ConstantAggregateBuilderBase::addSignedPointer(llvm::Constant *pointer, llvm::ConstantInt::get(IGM().Int64Ty, otherDiscriminator)); } +llvm::ConstantInt *IRGenModule::getMallocTypeId(llvm::Function *fn) { + return getDiscriminatorForString(*this, fn->getName()); +} + llvm::ConstantInt* IRGenFunction::getMallocTypeId() { - return getDiscriminatorForString(IGM, CurFn->getName()); + return IGM.getMallocTypeId(CurFn); } diff --git a/lib/IRGen/IRGenModule.h b/lib/IRGen/IRGenModule.h index c399e59d81bbf..8c67a59583a6c 100644 --- a/lib/IRGen/IRGenModule.h +++ b/lib/IRGen/IRGenModule.h @@ -1674,6 +1674,8 @@ private: \ llvm::AttributeList constructInitialAttributes(); StackProtectorMode shouldEmitStackProtector(SILFunction *f); + llvm::ConstantInt *getMallocTypeId(llvm::Function *fn); + void emitProtocolDecl(ProtocolDecl *D); void emitEnumDecl(EnumDecl *D); void emitStructDecl(StructDecl *D); From 83df3f5e7c2679718227a34c9c3ec1c340ddcb38 Mon Sep 17 00:00:00 2001 From: Nate Chandler Date: Wed, 29 Oct 2025 16:03:43 -0700 Subject: [PATCH 2/3] [IRGen] TypeID is 0 when typed-malloc is disabled. Produce a value of 0 when typed-malloc is disabled to avoid spurious materialization of type ids with more interesting values. --- lib/IRGen/GenPointerAuth.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/IRGen/GenPointerAuth.cpp b/lib/IRGen/GenPointerAuth.cpp index 588f395edfe95..66a6b6408036a 100644 --- a/lib/IRGen/GenPointerAuth.cpp +++ b/lib/IRGen/GenPointerAuth.cpp @@ -772,6 +772,12 @@ void ConstantAggregateBuilderBase::addSignedPointer(llvm::Constant *pointer, } llvm::ConstantInt *IRGenModule::getMallocTypeId(llvm::Function *fn) { + if (!getOptions().EmitTypeMallocForCoroFrame) { + // Even when typed malloc isn't enabled, a type id may be required for ABI + // reasons (e.g. as an argument to swift_coro_alloc). Use a cheaply + // materialized value. + return llvm::ConstantInt::get(Int64Ty, 0); + } return getDiscriminatorForString(*this, fn->getName()); } From f0f9b56e613dd665e5d4ab39090ec079f3482c1b Mon Sep 17 00:00:00 2001 From: Nate Chandler Date: Wed, 29 Oct 2025 17:33:55 -0700 Subject: [PATCH 3/3] [IRGen] Use 32-bit string hash for type id. --- lib/IRGen/GenPointerAuth.cpp | 4 +++- test/IRGen/yield_once_enable_emit_type_malloc_coro_frame.sil | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/IRGen/GenPointerAuth.cpp b/lib/IRGen/GenPointerAuth.cpp index 66a6b6408036a..a786a9b159420 100644 --- a/lib/IRGen/GenPointerAuth.cpp +++ b/lib/IRGen/GenPointerAuth.cpp @@ -778,7 +778,9 @@ llvm::ConstantInt *IRGenModule::getMallocTypeId(llvm::Function *fn) { // materialized value. return llvm::ConstantInt::get(Int64Ty, 0); } - return getDiscriminatorForString(*this, fn->getName()); + uint64_t hash = llvm::getStableSipHash(fn->getName()); + uint32_t hash32 = (hash % std::numeric_limits::max()) + 1; + return llvm::ConstantInt::get(Int64Ty, hash32); } llvm::ConstantInt* IRGenFunction::getMallocTypeId() { diff --git a/test/IRGen/yield_once_enable_emit_type_malloc_coro_frame.sil b/test/IRGen/yield_once_enable_emit_type_malloc_coro_frame.sil index 9eddb211dd261..9ae46e04a70cd 100644 --- a/test/IRGen/yield_once_enable_emit_type_malloc_coro_frame.sil +++ b/test/IRGen/yield_once_enable_emit_type_malloc_coro_frame.sil @@ -11,8 +11,8 @@ sil @marker : $(Builtin.Int32) -> () // CHECK-SAME: [[CORO_ATTRIBUTES:#[0-9]+]] sil @test_simple : $@yield_once () -> () { entry: - // CHECK-32: [[ID:%.*]] = call token (i32, i32, ptr, ptr, ptr, ptr, ...) @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], ptr %0, ptr @"$sIetA_TC", ptr @__swift_coroFrameAllocStub, ptr @free, i64 38223) - // CHECK-64: [[ID:%.*]] = call token (i32, i32, ptr, ptr, ptr, ptr, ...) @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], ptr %0, ptr @"$sIetA_TC", ptr @__swift_coroFrameAllocStub, ptr @free, i64 38223) + // CHECK-32: [[ID:%.*]] = call token (i32, i32, ptr, ptr, ptr, ptr, ...) @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], ptr %0, ptr @"$sIetA_TC", ptr @__swift_coroFrameAllocStub, ptr @free, i64 121146903) + // CHECK-64: [[ID:%.*]] = call token (i32, i32, ptr, ptr, ptr, ptr, ...) @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], ptr %0, ptr @"$sIetA_TC", ptr @__swift_coroFrameAllocStub, ptr @free, i64 121146903) // CHECK-NEXT: [[BEGIN:%.*]] = call ptr @llvm.coro.begin(token [[ID]], ptr null) // CHECK-NEXT: call swiftcc void @marker(i32 1000)