From ba8408458668368fd300774a98fe13873804afe4 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 01:24:23 +0000 Subject: [PATCH] Debug builds: the fewer-parameters thunk has a debug location; ___range_check is private to each module Two failures only a debug build sees, found building the debug default library as the release workflow does (`--di --opt_level=0`, TSLANG_DEBUG_CRT=/MT) and running its tests: - `a.every((v) => v < 40)` - since array callbacks take the element's index too, any one-parameter callback - was rejected by LLVM in every model: "inlinable function call in a function with a DISubprogram location must have a debug location". fewerParametersThunk (#733) made the thunk with no location; with --di the debug-info pass gave it a subprogram and its call had none. It now gets a subprogram at the place that first needs it and its body is scoped under it, as the catch copy thunk (mlirGenCatchCopyThunk) does. - A program linked against the debug default library failed with "LNK2005: ___range_check already defined": the RangeError check (#731) is generated from source as a public function in every module that checks, the library's included. It is private to each module now (internal linkage, as the thunks). An optimized link dropped one copy, which is why release builds linked. test-compile-debug-info-glue (debug-info-glue.cmake): the fewer-parameter tests emitted with `--di --opt_level=0` under every model - test-runner in a release build never compiles with --di. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenCast.cpp | 16 ++++++- tslang/lib/TypeScript/MLIRGenImpl.h | 8 ++++ tslang/test/tester/CMakeLists.txt | 8 ++++ tslang/test/tester/debug-info-glue.cmake | 56 ++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tslang/test/tester/debug-info-glue.cmake diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index c1424f5cb..c6404a11a 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -2199,11 +2199,25 @@ namespace mlirgen mlir::OpBuilder::InsertionGuard guard(builder); // compiler glue with no source to step through, as INSTANCEOF_NONE_NAME's - auto noLocation = mlir::UnknownLoc::get(builder.getContext()); + mlir::Location noLocation = mlir::UnknownLoc::get(builder.getContext()); auto funcOp = mlir_ts::FuncOp::create(noLocation, thunkName, thunkType); funcOp.setPrivate(); funcOp->setAttr("internal_linkage", builder.getUnitAttr()); + // ...but with --di a function of its own to the debugger, as the catch copy thunk is + // (mlirGenCatchCopyThunk): its call into the callee needs a location, or LLVM rejects the + // module ("inlinable function call in a function with a DISubprogram location must have a + // debug location"). A subprogram for the thunk at the place that first needs it, the body + // scoped under it. + DITableScopeT debugFuncScope(debugScope); + if (compileOptions.generateDebugInfo) + { + MLIRDebugInfoHelper mdi(builder, debugScope); + auto plainLocation = stripMetadata(location); + funcOp->setLoc(mdi.getSubprogram(plainLocation, thunkName, thunkName, plainLocation)); + noLocation = locFuseWithScope(plainLocation); + } + auto &entryBlock = *funcOp.addEntryBlock(); builder.setInsertionPointToStart(&entryBlock); diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 7482c9df6..700366b0c 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -7927,6 +7927,14 @@ class MLIRGenImpl { return mlir::failure(); } + + // a copy in each module that checks, its own: the default library has one as well, and two + // public ones were a duplicate symbol where nothing optimized one away (a debug build's link) + if (auto funcOp = theModule.lookupSymbol(funcName)) + { + funcOp.setPrivate(); + funcOp->setAttr("internal_linkage", builder.getUnitAttr()); + } } auto funcResult = resolveIdentifier(location, funcName, genContext); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 555bd1e46..0464e37b5 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2913,6 +2913,14 @@ add_test(NAME test-compile-debug-info-opt -DTESTS_DIR=${PROJECT_SOURCE_DIR}/test/tester/tests -P ${CMAKE_CURRENT_SOURCE_DIR}/debug-info-opt.cmake) +# Debug info without optimisation, for the functions the compiler makes itself - see +# debug-info-glue.cmake. +add_test(NAME test-compile-debug-info-glue + COMMAND ${CMAKE_COMMAND} + -DTSLANG=$ + -DTESTS_DIR=${PROJECT_SOURCE_DIR}/test/tester/tests + -P ${CMAKE_CURRENT_SOURCE_DIR}/debug-info-glue.cmake) + # A throw under the JIT without TypeScriptRuntime (which test-runner always passes), under every # model but gc - see jit-throw-without-runtime.cmake. Windows only: the symbol is MSVC's type_info vtable. if (WIN32) diff --git a/tslang/test/tester/debug-info-glue.cmake b/tslang/test/tester/debug-info-glue.cmake new file mode 100644 index 000000000..20a5c1fbe --- /dev/null +++ b/tslang/test/tester/debug-info-glue.cmake @@ -0,0 +1,56 @@ +# Emits LLVM IR WITH debug info and no optimisation, as a debug build compiles (`--di --opt_level=0`), +# under every memory model, for programs that make functions of the compiler's own: glue with no source +# of its own, which still has to come out with a debug location on everything that calls. +# +# `test-runner` in a release build compiles with `--opt` and no `--di`, so it never sees these; the +# release workflow's debug default library does. A function taking fewer parameters than its function +# type is called through a thunk (fewerParametersThunk), made with no location: with `--di` it got a +# subprogram while its call had none, and LLVM rejected the module - "inlinable function call in a +# function with a DISubprogram location must have a debug location". Array callbacks take the +# element's index too, so `a.every((v) => ...)` made one in every debug program that used them. +# +# A compile, not a run, for the same reason as debug-info-rc.cmake: the failure was a module that +# would not come out. + +if(NOT DEFINED TSLANG OR NOT DEFINED TESTS_DIR) + message(FATAL_ERROR "TSLANG and TESTS_DIR are both required") +endif() + +set(files + # a named function and an arrow given as a function type with more parameters + 00function_fewer_parameters.ts + # an arrow that captures, bound over its captures to the thunk + 00closure_fewer_parameters.ts) + +set(failures "") +set(count 0) +foreach(model gc rc none own) + foreach(name ${files}) + execute_process( + COMMAND "${TSLANG}" --emit=llvm --di --opt_level=0 -mm=${model} --no-default-lib + "${TESTS_DIR}/${name}" -o=${name}.${model}.ll + OUTPUT_QUIET + ERROR_VARIABLE diagnostics + RESULT_VARIABLE status) + + if(NOT status EQUAL 0) + # the first error, not a warning printed ahead of it + if(diagnostics MATCHES "[^\n]*error:[^\n]*") + set(first_line "${CMAKE_MATCH_0}") + else() + string(REGEX REPLACE "\n.*" "" first_line "${diagnostics}") + endif() + list(APPEND failures " ${name} -mm=${model}: exit ${status}: ${first_line}") + endif() + + file(REMOVE "${name}.${model}.ll") + math(EXPR count "${count} + 1") + endforeach() +endforeach() + +if(failures) + string(REPLACE ";" "\n" report "${failures}") + message(FATAL_ERROR "debug info without optimisation failed:\n${report}") +endif() + +message(STATUS "debug info without optimisation: ${count} compile(s) emitted")