Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV] Add CORE-V headers #78

Closed
wants to merge 1 commit into from

Conversation

melonedo
Copy link
Contributor

@melonedo melonedo commented Aug 27, 2023

Add header files for all CORE-V extensions that have C builtin functions following D155647. The definitions of existing builtin functions are also adjusted accordingly.

Note that D155647 defines new C functions without prefix __builtin (see below, also refer to RISCV-C-API Specification#Intrinsic Functions), while this PR simply re-declares existing builtins.

static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__))
__riscv_rol_32(uint32_t __x, uint32_t __y) {
  return __builtin_rotateleft32(__x, __y);
}

The header files are extracted from the specification with this script:

script
import re
from pathlib import Path
from textwrap import dedent

# identify target-independent builtin functions
NAME_TO_EXTENSION_MAP = dict(abs="alu")

with open("corev-builtin-spec.md", "rt") as file:
    spec = file.read()
exts = dict()

# rely on "####" as an indication of a builtin
for f in re.finditer(r"#### `(.+)`", spec):
    r = re.match(
        r"\w+ __builtin_((?:riscv_cv_([^_]+))?\w+)\s*\(.+\)",
        f[1],
    )
    if r is None:
        print("Failed to parse:", f[1])
        continue
    name = r[1]
    ext = NAME_TO_EXTENSION_MAP.get(name, r[2])
    # print(name, ext)
    exts.setdefault(ext, []).append(f[1])

include = Path("include")
include.mkdir(exist_ok=True)

for e, funcs in exts.items():
    prologue = dedent(f"""\
    #ifndef __RISCV_COREV_{e.upper()}_H
    #define __RISCV_COREV_{e.upper()}_H

    #include <stdint.h>

    #if defined(__cplusplus)
    extern "C" {{
    #endif

    #if defined(__riscv_xcv{e})
    """)
    epilogue = dedent(f"""\
    #endif // defined(__riscv_xcv{e})

    #if defined(__cplusplus)
    }}
    #endif

    #endif // define __RISCV_COREV_{e.upper()}_H
    """)
    ext_header = include / f"riscv_corev_{e}.h"
    with ext_header.open("wt") as file:
        file.write(prologue)
        for func in funcs:
            # file.write(
            #     "\nstatic __inline__ uint64_t __attribute__((__always_inline__, __nodebug__))\n"
            # )
            file.write(func)
            file.write(";\n")
        file.write(epilogue)

Fix #74.
CC @MaryBennett, @PaoloS02.

Add header files for all CORE-V extensions that have C builtin functions.
The definitions of existing builtin functions are also adjusted accordingly.
@melonedo
Copy link
Contributor Author

The failure on Mac happens in some python scripts and should be irrelevant, see https://reviews.llvm.org/D41784.

@@ -0,0 +1,38 @@
#ifndef __RISCV_COREV_ALU_H
Copy link
Contributor

Choose a reason for hiding this comment

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

Licence?

@ChunyuLiao
Copy link
Contributor

funan back to school , I think we can close this.

@PaoloS02 PaoloS02 mentioned this pull request Nov 9, 2023
@PaoloS02
Copy link
Contributor

PaoloS02 commented Jan 7, 2024

Implemented in new patches.

@PaoloS02 PaoloS02 closed this Jan 7, 2024
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.

CORE-V C API Specification
3 participants