Skip to content

all: add GORISCV64 environment variable #61476

Closed
@markdryan

Description

@markdryan

Overview

Go currently targets the RV64G instruction set when building for GOARCH=riscv64. This gives the compiler and hand-coded assembly language functions in the runtime and standard library access to scalar integer, multiply, divide, scalar floating point, atomic and fence instructions and some counters, but little else. RISC-V does define a number of extensions that could be used in the compiler (and the standard library) to improve performance and reduce code size. For example, the Bit Manipulation extensions could be used to speed up array access and implement an intrinsic for math/bits.OnesCount. The Vector extensions could be used to improve some of the hand-coded assembly language functions, such as memmove. However, the compiler cannot currently use those extensions without a runtime check (which it can't currently do either as it happens) as it needs to generate code that will run safely on all RV64G devices.

Proposal 61416 x/sys/unix, x/sys/cpu: use the RISC-V Hardware Probing Interface on Linux proposes a method of determining which extensions exist at runtime (on Linux). This proposal suggests a method to determine the existence of useful extensions at compile time. Similar mechanisms exists for other architectures supported by Go and are implemented via the use of an environment variable, e.g., GOAMD64. Here we propose adding a new RISC-V specific environment variable, GORISCV64, that could be used by the compiler and assembly language functions to determine which extensions can be safely used without runtime checks.

Suggested values for GORISCV64

RISC-V defines a set of profiles. Each of these profiles specify a base ISA and a set of mandatory extensions. For example, devices supporting the RVA20U64 profile must support RV64GC providing access to the compressed instruction set. Devices supporting RVA22U64 must additionally support Zba, Zbb and Zbs. RVA23U64 (which is not yet ratified) currently mandates the Vector extension and Pointer Masking.

This proposal recommends using the names of these standardised profiles, converted to lowercase, as valid values for the GORISCV64 environment variable, with one exception. There is no profile that corresponds to what the compiler currently targets, RV64G, so RV64G would also be a valid value of GORISCV64 and the default.

In summary, we would have

GORISCV64 Value buildcfg.GORISCV64 Build Tags Potential Benefits
rv64g 0 riscv64.rv64g The default and what Go currently targets
rva20u64 20 riscv64.rv64g, riscv64.rva20u64 Would allow the compiler to generate compressed instructions
rva22u64 22 riscv64.rv64g, riscv64.rva20u64, riscv64.rva22u64 Would allow the compiler to generate Zba, Zbb and Zbs instructions without runtime checks

Once RVA23U64 is ratified a fourth permissable value could be supported

GORISCV64 Value buildcfg.GORISCV64 Build Tags Potential Benefits
rva23u64 23 riscv64.rv64g, riscv64.rva20u64, riscv64.rva22u64, riscv64.rva23u64 Would potentially allow the use of vector instructions in hand written assembler without runtime checks. Pointer masking might be useful.

This proposal just covers the addition of the new environment variable. Extra work would be needed to take advantage of this variable such as adding support in the assembler for the extensions mandated by the profiles and updating the compiler and standard library routines to use them. I'll enter separate issues for these items.

Open Issues

This section will capture issues raised in the discussion below that do not yet have an agreed resolution.

  • Should the default value of GORISCV64 be rv64g (what the compiler currently targets and isn't actually a profile) or rva20u64 which makes the compressed instruction set mandatory?
  • Should GORISCV64 be restricted to rv64g and the profile names only (or perhaps just the profile names) or should we allow users to specify a complete ISA string? Complete ISA strings allow individual extensions to be specified and can include profile names but introduce additional complexity.
  • The current builders only support rv64g and rva20u64. These builders do support some but not all of the mandatory extensions in rva22u64. Restricting GORISCV64 to profile names (and rv64g) only will delay improvements to the compiler that can take advantage of these extensions, notably, Zba and Zbb, until we have builders that support rva22u64.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions