Skip to content

fix(sv): resolve input port patterns after specialization - #1772

Merged
mergify[bot] merged 1 commit into
masterfrom
stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7
Oct 23, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7

Conversation

@tignear

@tignear tignear commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Connecting an untyped pattern such as .cfg('{8{'0}}) failed before the receiving
port's shape was available. Preserve caller values, keys and repetition counts
until the module or interface input type is specialized, then lower the pattern
in that assignment context.

Share assignment conversion between native typed and deferred patterns. Convert
two-state members individually, consume outer packed dimensions before struct
fields, and disambiguate name keys against the receiving shape. Retain nominal
aggregate operand types through conditionals and function calls before inlining.
Use one typed value representation for constant and runtime operands; preserve
source sign and type before folding and substitute stored aggregate constants.
This prevents default struct constants such as 8'h35 becoming 8'h55 and retains
signed extension and mixed-state members. Reject unsupported recursive defaults
for packed unions instead of concatenating their overlapping fields.

Allow assignment-compatible unpacked subarrays as explicit pattern items. Keep
the stricter type-matching rule for selecting whole defaults: IEEE 1800-2023
6.22.1(f) requires matching fixed-array bounds, and 10.9.1 uses matching types
there. Review r4354410020's proposed relaxation is not adopted; Slang independently
rejects its row[4:5] default for cfg[2][0:1]. Explicit differently bounded array
items remain accepted. Preliminary function signatures supply types and signs
for calls inside typed patterns.

Add eighteen shared cases covering specialization, caller scope, mixed states,
aggregates, interfaces, collisions, nominal identity, expressions and constants.
Add API rejection checks for incompatible subarrays, array-to-scalar items and
unsupported union defaults. The latest P1 reproduction fails on all five
backends against the published analyzer, returning 0x5555 instead of 0x3535.

Validation:

  • Full SV: 4104 passed, 20 existing ignored across five backends.
  • Analyzer: 791 passed, 3 existing ignored; frontend: 4 passed.
  • Shared suite default-feature library: 30 passed.
  • Relevant library/harness Clippy -D warnings, fmt and diff checks pass.
  • Verilator passes the latest packed constant matrix, including signed extension,
    and the minimal literal case. The mixed-state adapter is unsupported and
    Verilator rejects valid unpacked constant rows. The mismatched-bounds case is
    rejected but classified as a compile error by the adapter. Earlier results
    include the distinct-struct default discrepancy and unpacked-item limits.
  • Slang 12.0 corroborates the latest ordinary aggregates, literal values, mixed
    states, unpacked rows and rejection of mismatched default bounds. One signed
    struct pattern-initializer discrepancy remains recorded: it zero-extends a
    signed 0xf5 value. IEEE 7.2.1/10.9.1 and Verilator support sign extension.
  • Icarus cannot compile the added pattern/interface cases. External limitations
    and disagreements are not passes; no exclusions or expectations were changed.
  • Replayed all 20 formerly pattern-blocked Taxi configurations against the final
    implementation. The next blockers remain $size x9, interface handles x6,
    undriven net x4, non-integer override x1; none reaches cocotb. All 199 resolved
    SV sources match the original audit. No RTL, optimization or diagnostic-policy
    changes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T00:06:19.008984Z 78aeed1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0bb163ebfa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +167 to +170
Self::Runtime { expr, signed } => (
expr,
signed.ok_or_else(|| unsupported("assignment pattern operand signedness"))?,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Coerce each two-state pattern element

When a port pattern targets a mixed packed struct such as struct packed { bit [3:0] a; logic [3:0] b; }, this runtime branch only resizes each value and ignores the selected member shape's is_2state. An input like .cfg('{a:'x, b:'x}) therefore leaves X bits in a; the later whole-port conversion cannot fix this because the aggregate is four-state due to b. SystemVerilog requires X/Z assigned to a bit member to become zero, so apply ToTwoState while lowering each two-state element.

Useful? React with 👍 / 👎.

tree: &AnalysisContext<'_>,
dims: &PackedDimensions,
) -> Converted<BoundPattern> {
match binding::value_mode(root, tree, dims, false)? {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Defer member-key disambiguation until the target is known

When a structure member name also exists as a constant in the caller, parsing the pattern here without a target misclassifies the member key as an array index. For example, with caller localparam tag = 0 and a child struct member named tag, .cfg('{tag: value, default: 0}) stores Key::Index(0) because prepare_native receives shape=None; binding against the specialized struct later rejects that key instead of assigning its tag member. Preserve ambiguous name keys until the receiving shape can distinguish members from constant indices.

Useful? React with 👍 / 👎.

Comment on lines +107 to +109
let mut connection = PortConnection::new(formal.clone(), formal, None);
connection.bound_pattern = Some(bound_patterns::from_port(root, tree, dims)?);
return Ok(connection);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Bind patterns on interface input ports before dropping expressions

For an interface instance with an aggregate input port, such as I i(.cfg('{default:'1})), this path replaces the connection's actual_expr with only bound_pattern. Interface elaboration in ast/interface_binding.rs consumes actual_expr directly and skips connections where it is None, while interface declarations are subsequently removed from ordinary module-instance lowering, so bind_port_patterns never gets a chance to restore the expression. The interface input is therefore left unassigned or rejected; resolve the pattern during interface elaboration or carry it through that path.

Useful? React with 👍 / 👎.

Comment on lines +94 to +98
pub(crate) fn bind_port(&self, target: &Type) -> Converted<crate::ir::Expr> {
let state = AnalysisState::default();
let shape = procedural::dimensions_from_type(target);
self.lower(&state, &shape, &PackedDimensions::default())
.map(Into::into)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Descend outer packed dimensions before struct members

When the input type is a packed array of a packed struct, procedural::dimensions_from_type retains both the outer packed ranges and the struct members, but BoundPattern::lower checks shape.members before the outer packed dimension. Thus for typedef struct packed { logic a, b; } S; input S [1:0] cfg, .cfg('{default:'1}) is lowered as one two-bit struct instead of two struct elements; glue then widens that value to four bits, producing 4'b0011 rather than 4'b1111. The binding path needs to consume packed dimensions enclosing the aliased struct before selecting its members.

Useful? React with 👍 / 👎.

@tignear
tignear force-pushed the stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7 branch from 0bb163e to 110e998 Compare October 22, 2026 22:21
@tignear

tignear commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Revision history

# Type Changes Reason Date
1 initial 0bb163e 2026-10-22 22:20 UTC
2 content 0bb163e → 110e998 Fix the three P1 findings and P2 key disambiguation from the initial review. Add regression coverage for typed/deferred patterns, mixed-state members, packed struct arrays, module/interface inputs, a… 2026-10-22 22:20 UTC
3 content 110e998 → 8153322 Address the next operand-type review findings together: retain packed-array and conditional/function types, allow only assignment-compatible subarray items, and expand regression/reference coverage b… 2026-10-22 22:52 UTC
4 content 8153322 → 78aeed1 Fix constant aggregate type/sign loss and recursive packed-union defaults; unify constant/runtime operand metadata and retain the standard-mandated rejection of mismatched-bound defaults with indepen… 2026-10-23 00:00 UTC

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 110e998798

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +174 to +175
.filter(|shape| !shape.members.is_empty() || !shape.unpacked.is_empty())
.and_then(|shape| shape.pattern_type(&tree.state, dims)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Retain packed-array types when deciding default broadcast

When a default item supplies a value of the exact same multidimensional packed type as an unpacked target element, this filter discards its type because it has neither members nor unpacked dimensions. For example, with typedef logic [1:0][3:0] pair_t; input pair_t cfg[2], .cfg('{default:value}) should copy an 8-bit pair_t value into each cfg element, but assignment_value treats it as an untyped scalar, descends the outer packed dimension, and duplicates the low nibble (turning 8'hAB into 8'hBB). Preserve packed aggregate types here so an exact element-type match is assigned as a whole.

Useful? React with 👍 / 👎.

Comment on lines 184 to 187
if !default && !shape.unpacked.is_empty() {
return Err(unsupported(
"scalar assignment pattern item for unpacked array",
));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Permit array-valued items in outer pattern lists

When the target has multiple unpacked dimensions, an outer list item may itself be an expression of the remaining subarray type, but this unconditional check rejects it before the captured value_type can be compared or lowered. For example, given byte row[2] and a port input byte cfg[2][2], the valid connection .cfg('{row, row}) is rejected as a scalar item even though each row exactly matches one outer element. Allow matching unpacked-array values to be assigned as a whole and reserve this error for genuinely scalar or incompatible operands.

Useful? React with 👍 / 👎.

) {
patterns::typed_shape(root, tree, dims)?
} else {
dimensions::operand_shape(root, tree, dims)?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve aggregate types through conditional pattern operands

When both branches of a conditional have the same packed-struct type, the conditional result retains that aggregate type, but operand_shape reduces the mux to a scalar width here and native_value_type consequently returns None. For item_t a, b and a port input item_t cfg[2], .cfg('{default:(sel ? a : b)}) should replicate the selected struct into both elements; instead assignment_value treats the value as nonmatching and broadcasts its low member-sized bits into every field, turning a selected 8'h35 into 8'h55. Preserve the common aggregate type of conditional operands before deciding whether to descend the default.

Useful? React with 👍 / 👎.

@tignear
tignear force-pushed the stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7 branch from 110e998 to 8153322 Compare October 22, 2026 22:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81533222a0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +120 to +121
values.push(if constant {
Value::Expression(frozen(&tree.state, expr, dims)?)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve types for constant aggregate defaults

When a parameter override uses an aggregate constant as a default item, such as localparam item_t V = '{a:3,b:5}; Child #(.P('{default:V})), this branch freezes the expression into Value::Expression without retaining item_t. The new recursive default lowering therefore treats V as untyped and broadcasts its low member-sized bits into every member, producing 8'h55 instead of copying 8'h35 into each item_t. Retain the aggregate type alongside frozen constant values so exact-type defaults are assigned as a whole.

Useful? React with 👍 / 👎.

Comment on lines +133 to +134
let default = default
&& !value_type.is_some_and(|ty| shape.pattern_type(state, dims).as_ref() == Some(ty));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Compare unpacked defaults by assignment compatibility

When an unpacked-array default has compatible element counts but different index bounds, raw Type equality fails here even though the arrays are assignment-compatible. For example, a byte row[4:5] used in .cfg('{default:row}) for input byte cfg[2][0:1] should copy row into each outer element; instead this keeps default set, descends into the target subarray, and eventually rejects row as an array value for a scalar. Use unpacked-array assignment compatibility, rather than exact range equality, when deciding whether to descend.

Useful? React with 👍 / 👎.

Comment on lines +173 to +174
if !shape.members.is_empty() {
return Ok(Expr::Concat(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject recursive defaults for packed unions

When an outer array pattern supplies a default for a packed-union element, this branch treats the overlapping union members as independent struct fields and concatenates values for all of them, bypassing the packed-union rejection used when the union is the direct pattern target. For a supported equal-width mixed-state union such as union packed { logic a; bit b; } inside an array, '{default:'x} produces {1'bx,1'b0} per union and downstream width coercion can retain only the final zero bit. Detect the all-zero member offsets here and reject the unsupported union default instead of constructing a value wider than the union.

Useful? React with 👍 / 👎.

@mergify

mergify Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

Connecting an untyped pattern such as .cfg('{8{'0}}) failed before the receiving
port's shape was available. Preserve caller values, keys and repetition counts
until the module or interface input type is specialized, then lower the pattern
in that assignment context.

Share assignment conversion between native typed and deferred patterns. Convert
two-state members individually, consume outer packed dimensions before struct
fields, and disambiguate name keys against the receiving shape. Retain nominal
aggregate operand types through conditionals and function calls before inlining.
Use one typed value representation for constant and runtime operands; preserve
source sign and type before folding and substitute stored aggregate constants.
This prevents default struct constants such as 8'h35 becoming 8'h55 and retains
signed extension and mixed-state members. Reject unsupported recursive defaults
for packed unions instead of concatenating their overlapping fields.

Allow assignment-compatible unpacked subarrays as explicit pattern items. Keep
the stricter type-matching rule for selecting whole defaults: IEEE 1800-2023
6.22.1(f) requires matching fixed-array bounds, and 10.9.1 uses matching types
there. Review r4354410020's proposed relaxation is not adopted; Slang independently
rejects its row[4:5] default for cfg[2][0:1]. Explicit differently bounded array
items remain accepted. Preliminary function signatures supply types and signs
for calls inside typed patterns.

Add eighteen shared cases covering specialization, caller scope, mixed states,
aggregates, interfaces, collisions, nominal identity, expressions and constants.
Add API rejection checks for incompatible subarrays, array-to-scalar items and
unsupported union defaults. The latest P1 reproduction fails on all five
backends against the published analyzer, returning 0x5555 instead of 0x3535.

Validation:
- Full SV: 4104 passed, 20 existing ignored across five backends.
- Analyzer: 791 passed, 3 existing ignored; frontend: 4 passed.
- Shared suite default-feature library: 30 passed.
- Relevant library/harness Clippy -D warnings, fmt and diff checks pass.
- Verilator passes the latest packed constant matrix, including signed extension,
  and the minimal literal case. The mixed-state adapter is unsupported and
  Verilator rejects valid unpacked constant rows. The mismatched-bounds case is
  rejected but classified as a compile error by the adapter. Earlier results
  include the distinct-struct default discrepancy and unpacked-item limits.
- Slang 12.0 corroborates the latest ordinary aggregates, literal values, mixed
  states, unpacked rows and rejection of mismatched default bounds. One signed
  struct pattern-initializer discrepancy remains recorded: it zero-extends a
  signed 0xf5 value. IEEE 7.2.1/10.9.1 and Verilator support sign extension.
- Icarus cannot compile the added pattern/interface cases. External limitations
  and disagreements are not passes; no exclusions or expectations were changed.
- Replayed all 20 formerly pattern-blocked Taxi configurations against the final
  implementation. The next blockers remain $size x9, interface handles x6,
  undriven net x4, non-integer override x1; none reaches cocotb. All 199 resolved
  SV sources match the original audit. No RTL, optimization or diagnostic-policy
  changes.

Change-Id: Icbecc7d790d68db2fa484738d78188f5afdbf442
@tignear
tignear force-pushed the stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7 branch from 8153322 to 78aeed1 Compare October 23, 2026 00:00
@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 23, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 23, 2026
@mergify

mergify Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-23 01:00 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • ✅ Checks passed · on draft merge queue: checking #1772 on master (5734299) #1785
  • ✅ Merged — 2026-10-23 02:22 UTC · at 672971cf3145ef7fd7e70572b2311d589b07f62b · merge

This pull request spent 1 hour 21 minutes 59 seconds in the queue, including 1 hour 20 minutes 36 seconds running CI.

Required conditions to merge
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify mergify Bot mentioned this pull request Oct 23, 2026
40 of 71 tasks
@mergify
mergify Bot merged commit 672971c into master Oct 23, 2026
30 checks passed
@mergify
mergify Bot deleted the stack/tignear/fix/sv-pattern-target-context/resolve-input-port-patterns-specialization--cbecc7d7 branch October 23, 2026 02:22
@mergify mergify Bot removed the queued Mergify queue entry label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant