[BugFix][TIRx] Fix bad-optional-access in BF16/FP8 legalize passes for target-less PrimFuncs#19383
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the BF16 and FP8 legalization passes to safely handle functions where the target attribute is missing, preventing potential crashes. However, the current implementation returns early when a target is undefined, which inadvertently skips legalization for target-less functions. Feedback suggests combining the target existence check with the capability check to ensure consistency with host targets and to allow the legalizer to run on functions without an explicit target.
…r target-less PrimFuncs BF16ComputeLegalize, BF16StorageLegalize, FP8ComputeLegalize, and FP8StorageLegalize all called f->GetAttr<Target>(kTarget).value() unconditionally. Host-side helper PrimFuncs produced during Relax lowering carry no target attribute; .value() on an empty Optional<Target> aborts at runtime with a bad optional access. Check opt_target.defined() and return f unchanged when absent. Host-only functions do not use BF16/FP8 on device and need no legalization.
532f75b to
7fa7c8f
Compare
tlopex
approved these changes
Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BF16ComputeLegalize,BF16StorageLegalize,FP8ComputeLegalize, andFP8StorageLegalizeall callf->GetAttr<Target>(kTarget).value()unconditionally. Host-side helper PrimFuncs produced during Relax lowering (e.g. forreshape,mean) carry no target attribute;.value()on an emptyOptional<Target>aborts at runtime:This surfaces whenever
tvm.compiletargets CUDA on a model with BF16/FP8 support compiled in, because the Relax lowering pipeline mixes target-annotated GPU kernels with target-less CPU helpers in the same module.Fix
Retrieve the target into
opt_targetand combine thedefined()check withCheckDataTypeSupport:This is consistent with how explicit host targets (e.g.
llvm) are handled, which also go through legalization. Applied identically to all four legalize pass lambdas.