gemini-code-assist[bot] commented on code in PR #19806:
URL: https://github.com/apache/tvm/pull/19806#discussion_r3425142017
##########
include/tvm/tirx/op.h:
##########
@@ -739,7 +739,7 @@ inline void CheckMathUnaryOpInputDType(const char* op_name,
DataType dtype) {
// Intrinsic operators
#define TVM_DECLARE_INTRIN_UNARY_WITH_CHECK(OpName, CheckInputDType) \
inline PrimExpr OpName(PrimExpr x, Span span = Span()) { \
- static const Op& op = Op::Get("tirx." #OpName); \
+ static const Op op = Op::Get("tirx." #OpName); \
Review Comment:

Storing the `Op` by value as `static const Op op` introduces a non-trivial
static local object. Since `Op` is a reference-counted smart pointer
(`ffi::ObjectRef`), its destructor will run at program exit. This can lead to a
**static destruction order fiasco** (crash-on-exit) if the TVM/FFI runtime or
global registry is deinitialized before these static local instances are
destroyed.
To avoid both the GCC `-Wdangling-reference` warning and the static
destruction order issue, we can use the `*&` pattern:
`static const Op& op = *&Op::Get("tirx." #OpName);`
This takes the address of the reference returned by `Op::Get` (which is
guaranteed to be valid for the lifetime of the program) and dereferences it.
This bypasses GCC's lifetime analysis warning while keeping `op` as a
reference, preventing any copy construction or static destruction issues.
```c
static const Op& op = *&Op::Get("tirx." #OpName);
\
```
##########
include/tvm/tirx/op.h:
##########
@@ -786,7 +786,7 @@ TVM_DECLARE_INTRIN_UNARY(clz);
#define TVM_DECLARE_INTRIN_BINARY(OpName) \
inline PrimExpr OpName(PrimExpr x, PrimExpr y, Span span = Span()) { \
- static const Op& op = Op::Get("tirx." #OpName); \
+ static const Op op = Op::Get("tirx." #OpName); \
Review Comment:

Storing the `Op` by value as `static const Op op` introduces a non-trivial
static local object. Since `Op` is a reference-counted smart pointer
(`ffi::ObjectRef`), its destructor will run at program exit. This can lead to a
**static destruction order fiasco** (crash-on-exit) if the TVM/FFI runtime or
global registry is deinitialized before these static local instances are
destroyed.
To avoid both the GCC `-Wdangling-reference` warning and the static
destruction order issue, we can use the `*&` pattern:
`static const Op& op = *&Op::Get("tirx." #OpName);`
This takes the address of the reference returned by `Op::Get` (which is
guaranteed to be valid for the lifetime of the program) and dereferences it.
This bypasses GCC's lifetime analysis warning while keeping `op` as a
reference, preventing any copy construction or static destruction issues.
```c
static const Op& op = *&Op::Get("tirx." #OpName); \
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]