Skip to content

ssa: emit go semantic metadata#1728

Open
luoliwoshang wants to merge 10 commits intogoplus:mainfrom
luoliwoshang:codex/useiface-metadata-producer
Open

ssa: emit go semantic metadata#1728
luoliwoshang wants to merge 10 commits intogoplus:mainfrom
luoliwoshang:codex/useiface-metadata-producer

Conversation

@luoliwoshang
Copy link
Copy Markdown
Member

@luoliwoshang luoliwoshang commented Mar 18, 2026

Summary

  • add package-level deadcode metadata emitter infrastructure in SSA
  • emit !llgo.useiface, !llgo.useifacemethod, and !llgo.methodoff
  • encode metadata entries by symbol name so emission does not force extra LLVM globals or ABI type descriptors into the module
  • regenerate existing IR golden files via chore/gentests

Validation

  • env GOCACHE=/tmp/llgo-gocache go test ./ssa -run '^$'
  • env GOCACHE=/tmp/llgo-gocache go run ./chore/gentests
  • env GOCACHE=/tmp/llgo-gocache go test ./cl -run 'TestFromTestgo/(ifaceconv|invoke|abimethod|interface)$' -count=1

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the LLGo compiler's SSA generation by adding a mechanism to emit semantic metadata. Specifically, it now tracks and annotates instances where concrete types are converted to interface types using !llgo.useiface metadata. This change provides more detailed information in the generated LLVM IR, which can be beneficial for downstream analysis or optimization passes. The update also includes the necessary regeneration of test output files to align with this new metadata emission.

Highlights

  • Semantic Metadata Emission: Introduced a new package-level semantic metadata emitter infrastructure within the SSA (Static Single Assignment) form generation.
  • Interface Conversion Metadata: Implemented the emission of !llgo.useiface metadata for non-interface to interface conversions originating from the MakeInterface operation.
  • Test File Regeneration: Regenerated numerous existing IR golden files to reflect the newly introduced metadata, ensuring consistency and correctness.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@xgopilot
Copy link
Copy Markdown
Contributor

xgopilot bot commented Mar 18, 2026

Overall: Clean, well-scoped PR. The semantic metadata emitter is a solid foundation for recording interface conversions. The golden file updates are consistent. A few items below worth addressing — mainly a redundant abiTypeGlobal call and a missing license header.

ssa/interface.go Outdated
Comment on lines 120 to 123
if _, ok := types.Unalias(typ.raw.Type).Underlying().(*types.Interface); !ok {
b.Pkg.emitUseIface(b.Func.impl, b.abiTypeGlobal(typ.raw.Type).impl)
}
tabi := b.abiType(typ.raw.Type)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Performance: abiTypeGlobal(typ.raw.Type) is called here at line 121, then called again at line 123 via abiType (which internally calls abiTypeGlobal with the same argument). On first invocation per type this performs TypeName resolution + VarOf map lookup + potentially heavy global construction; on subsequent calls it still does the name computation and lookup.

Consider hoisting the call and reusing the result:

Suggested change
if _, ok := types.Unalias(typ.raw.Type).Underlying().(*types.Interface); !ok {
b.Pkg.emitUseIface(b.Func.impl, b.abiTypeGlobal(typ.raw.Type).impl)
}
tabi := b.abiType(typ.raw.Type)
if _, ok := types.Unalias(typ.raw.Type).Underlying().(*types.Interface); !ok {
b.Pkg.emitUseIface(b.Func.impl, b.abiTypeGlobal(typ.raw.Type).impl)
}
tabi := b.abiType(typ.raw.Type)

	g := b.abiTypeGlobal(typ.raw.Type)
	if _, ok := types.Unalias(typ.raw.Type).Underlying().(*types.Interface); !ok {
		b.Pkg.emitUseIface(b.Func.impl, g.impl)
	}
	tabi := Expr{llvm.ConstGEP(g.impl.GlobalValueType(), g.impl, []llvm.Value{...}), prog.AbiTypePtr()}

Or at minimum, ensure abiType can accept a precomputed Global to avoid the double lookup.

@@ -0,0 +1,51 @@
package ssa
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consistency: This file is missing the Apache 2.0 copyright header that every other .go file in ssa/ includes. Please add it for consistency:

/*
 * Copyright (c) 2024 The XGo Authors (xgo.dev). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * ...
 */

ssa/metadata.go Outdated
Comment on lines +40 to +42
func (p Package) emitUseIface(owner, target llvm.Value) {
if owner.IsNil() || target.IsNil() {
return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Robustness: The silent nil-guard here may mask bugs upstream. If b.Func.impl or the result of abiTypeGlobal is unexpectedly nil, this silently drops the metadata rather than surfacing the problem. Consider at least a debug-mode log/warning so these cases don't go unnoticed during development.

ssa/metadata.go Outdated
Comment on lines +44 to +48
p.semMeta.add(
p.mod,
llgoUseIfaceMetadata,
metadataKey(owner.Name(), target.Name()),
metadataSymbol(owner),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clarity / Correctness: The dedup key is built from owner.Name() and target.Name(). Two concerns:

  1. Key collision risk: If two distinct LLVM values share the same name (or both have empty names), the deduplication would incorrectly suppress a legitimate entry.
  2. A brief comment explaining the \x00 separator choice in metadataKey and add would help future maintainers understand this is safe because it cannot appear in symbol names.

@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch from 1958ac9 to 8e77b05 Compare March 18, 2026 06:58
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an infrastructure for emitting package-level semantic metadata in the SSA backend and uses it to emit !llgo.useiface metadata for non-interface to interface conversions. The implementation includes a new semanticMetadataEmitter to manage and prevent duplicate metadata entries in the LLVM module. The changes are well-structured, with a logical refactoring in abitype.go to support the new functionality in interface.go. The code is clean, correct, and the regenerated golden files confirm the intended behavior. Overall, this is a solid improvement to the compiler's metadata emission capabilities.

@luoliwoshang luoliwoshang changed the title Add llgo.useiface metadata emission ssa: emit llgo.useiface metadata Mar 18, 2026
@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch from 57e867d to b0f7c04 Compare March 18, 2026 08:53
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 96.47059% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.19%. Comparing base (aae5e97) to head (d394c0b).

Files with missing lines Patch % Lines
ssa/metadata.go 94.36% 2 Missing and 2 partials ⚠️
ssa/mdtest/metadata.go 92.85% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1728      +/-   ##
==========================================
+ Coverage   93.15%   93.19%   +0.03%     
==========================================
  Files          48       50       +2     
  Lines       13352    13511     +159     
==========================================
+ Hits        12438    12591     +153     
- Misses        727      731       +4     
- Partials      187      189       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch 4 times, most recently from 646581d to 1b2e89b Compare March 18, 2026 10:13
@luoliwoshang luoliwoshang changed the title ssa: emit llgo.useiface metadata ssa: emit deadcode metadata Mar 18, 2026
@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch 4 times, most recently from 728feba to a4b76a5 Compare March 19, 2026 02:47
Copy link
Copy Markdown
Member Author

@luoliwoshang luoliwoshang Mar 19, 2026

Choose a reason for hiding this comment

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

will use goplus/llvm instead

@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch 3 times, most recently from 3e35f66 to 7222776 Compare March 19, 2026 11:09
@luoliwoshang luoliwoshang changed the title ssa: emit deadcode metadata ssa: emit go semantic metadata Mar 19, 2026
@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch from 7222776 to 146a83e Compare March 20, 2026 01:32
@luoliwoshang luoliwoshang force-pushed the codex/useiface-metadata-producer branch 2 times, most recently from 3064a49 to b2b62bb Compare March 20, 2026 06:20
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

接口方法数组: 0 0 0 0
类型。 1 1 0 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant