Skip to content

Conversation

@kibels
Copy link

@kibels kibels commented Jan 19, 2026

The existing Csr.write(), Csr.set() and Csr.clear() functions only allow the "r" register constraint, so always generate as two instructions e.g.

li a0, 8
csrs mstatus, a0

By refining the implementation a bit to detect when the value is comptime known and small enough to fit in the u5 immediate, this change allows the compiler to generate single atomic instructions for small immediates, like when setting the global interrupt enable bits.

csrsi mstatus, 8

The interface is changed slightly, to accept the packed struct T directly for set/clear instead of an anonymous struct. This has some benefits like a known result type, and allows detection of whether the passed in value is comptime known which the get_bits() necessary for .modify() to work obscures.

A before/after of the codegen can be observed in: https://godbolt.org/z/sKeboeoWv

CSRs that are meant to be used with these interfaces should have default field values of 0, so that the set/clear mask can be generated without providing all of the fields.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

Found 2 issues on changed lines in 1 file:

  • modules/riscv32-common/src/riscv32_common.zig: 2 issues

@kibels kibels force-pushed the atomic_interrupt_enable branch from caeea33 to 990c234 Compare January 19, 2026 16:33
@github-actions github-actions bot dismissed their stale review January 19, 2026 16:33

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🔍 Lint Results

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.

3 participants