Merged
fix(pass): Identify sanitizer entry by function type instead of name #6767
fix(pass): Identify sanitizer entry by function type instead of name #6767
Merged
zhengyaoff12Add on Sep 29
Total: 2 files changed+49-3
@@ -545,15 +545,20 @@ ProgramPtr SanitizerInstrumenter::Instrument(ProgramPtr program)
545{545{
546 // Instrument the entry function only, appending the hidden546 // Instrument the entry function only, appending the hidden
547 // sanitizer_log (ptr) + sanitizer_log_capacity (scalar) parameters.547 // sanitizer_log (ptr) + sanitizer_log_capacity (scalar) parameters.
548+ // The entry is identified structurally, as the Opaque function the
549+ // frontend builds for the kernel body (the same criterion the host
550+ // side uses to locate the kernel entry), never by name: the program
551+ // name (e.g. from @pl.jit(name=...)) may differ from the entry
552+ // function name, and a name match would silently skip instrumentation
553+ // then, breaking the launch ABI.
548 // SIMT functions (SIMT_VF / SIMT_CALLEE) are outside the detection554 // SIMT functions (SIMT_VF / SIMT_CALLEE) are outside the detection
549 // scope: excluded by function type (the codegen's own criterion), not555 // scope: excluded by function type (the codegen's own criterion), not
550 // only by name -- a same-named SIMT function can never be mistaken556 // only by name -- a same-named SIMT function can never be mistaken
551 // for the entry.557 // for the entry.
552 std::map<std::string, FunctionPtr> new_functions;558 std::map<std::string, FunctionPtr> new_functions;
559+ uint32_t entry_count = 0;
553 for (const auto& [name, func] : program->functions_) {560 for (const auto& [name, func] : program->functions_) {
554- bool is_simt = func != nullptr &&561+ if (func == nullptr || func->funcType_ != FunctionType::OPAQUE) {
555- (func->funcType_ == FunctionType::SIMT_VF || func->funcType_ == FunctionType::SIMT_CALLEE);
556- if (func == nullptr || is_simt || func->name_ != program->name_) {
557 new_functions[name] = func;562 new_functions[name] = func;
558 continue;563 continue;
559 }564 }
@@ -572,7 +577,10 @@ ProgramPtr SanitizerInstrumenter::Instrument(ProgramPtr program)
572 params.push_back(capacity_var);577 params.push_back(capacity_var);
573 new_functions[name] = std::make_shared<Function>(func->name_, std::move(params), func->returnTypes_, body,578 new_functions[name] = std::make_shared<Function>(func->name_, std::move(params), func->returnTypes_, body,
574 func->span_, func->funcType_, func->entry_);579 func->span_, func->funcType_, func->entry_);
580+ ++entry_count;
575 }581 }
582+ PRO_PASS_INTERNAL_CHECK(npu::tile_fwk::InternalError::PASS_INNER_ERROR, entry_count == 1)
583+ << "sanitizer: expected exactly one Opaque entry function, found " << entry_count;
576 return std::make_shared<Program>(std::move(new_functions), program->name_, program->span_, program->debugInfo_);584 return std::make_shared<Program>(std::move(new_functions), program->name_, program->span_, program->debugInfo_);
577}585}
578 586 
@@ -1597,6 +1597,44 @@ def test_alias_rebind_oob_detected():
1597 logging.info("test_alias_rebind_oob_detected OK")1597 logging.info("test_alias_rebind_oob_detected OK")
1598 1598 
1599 1599 
1600+# -----------------------------------------------------------------------------
1601+# 十三、@pl.jit(name=...) 插桩回归(入口识别不依赖命名巧合)
1602+# -----------------------------------------------------------------------------
1603+ 
1604+ 
1605+@pl.jit(sanitizer=True, auto_mutex=True, name="sanitizer_named_entry_kernel")
1606+def named_clean_kernel(
1607+ x: pl.Tensor[[TILE_M, TILE_N], pl.DT_FP16],
1608+ z: pl.Tensor[[TILE_M, TILE_N], pl.DT_FP16],
1609+):
1610+ tt = pl.TileType(shape=[TILE_M, TILE_N], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec)
1611+ a = pl.make_tile(tt, addr=0x0000)
1612+ with pl.section_vector():
1613+ pl.load(a, x, [0, 0])
1614+ pl.system.sync_src(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0)
1615+ pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0)
1616+ pl.system.sync_src(set_pipe=pl.PipeType.V, wait_pipe=pl.PipeType.MTE3, event_id=1)
1617+ pl.system.sync_dst(set_pipe=pl.PipeType.V, wait_pipe=pl.PipeType.MTE3, event_id=1)
1618+ pl.store(z, a, [0, 0])
1619+ 
1620+ 
1621+@pytest.mark.soc("950")
1622+def test_named_kernel_sanitizer_instrumented():
1623+ """正样本:name= 与函数名不一致时仍应完成插桩并正常启动。
1624+ 
1625+ 回归:入口函数曾按 func->name_ == program->name_(命名巧合)识别,
1626+ @pl.jit(name=...) 与函数名不同时插桩被静默跳过、隐藏参数未追加,
1627+ launch 报 InvalidType: Expected 2 args, got 4。
1628+ """
1629+ _require_a5(ST_DEVICE)
1630+ x = _inputs(ST_DEVICE, [TILE_M, TILE_N])
1631+ z = torch.zeros([TILE_M, TILE_N], device=ST_DEVICE, dtype=torch.float16)
1632+ named_clean_kernel(x, z)
1633+ torch.npu.synchronize()
1634+ torch.testing.assert_close(z, x)
1635+ logging.info("test_named_kernel_sanitizer_instrumented OK")
1636+ 
1637+ 
1600if __name__ == "__main__":1638if __name__ == "__main__":
1601 """python3 直接执行入口:跑本文件全部 sanitizer 自研日志回放用例。1639 """python3 直接执行入口:跑本文件全部 sanitizer 自研日志回放用例。
1602 1640