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

[DRAFT][RISCV] Emit arch string macro to facilitate ASM programming #85063

Closed
wants to merge 1 commit into from

Conversation

nemanjai
Copy link
Member

In order to make assembly programming more convenient, emit macro __riscv_cmdline_arch_string that will be defined to the arch string based on the command line arguments. This string may differ from the actual string that is added to the object or assembly file. This provides a convenient mechanism for the programmer to add the .attribute directive into pre-processed asm files.

Example file.S:
.attribute 5, __riscv_cmdline_arch_string

In order to make assembly programming more convenient,
emit macro __riscv_cmdline_arch_string that will be defined
to the arch string based on the command line arguments.
This string may differ from the actual string that is
added to the object or assembly file. This provides a
convenient mechanism for the programmer to add the
.attribute directive into pre-processed asm files.

Example file.S:
.attribute 5, __riscv_cmdline_arch_string
@nemanjai nemanjai requested review from asb, topperc and wangpc-pp March 13, 2024 12:02
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:RISC-V clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-risc-v

Author: Nemanja Ivanovic (nemanjai)

Changes

In order to make assembly programming more convenient, emit macro __riscv_cmdline_arch_string that will be defined to the arch string based on the command line arguments. This string may differ from the actual string that is added to the object or assembly file. This provides a convenient mechanism for the programmer to add the .attribute directive into pre-processed asm files.

Example file.S:
.attribute 5, __riscv_cmdline_arch_string


Full diff: https://github.com/llvm/llvm-project/pull/85063.diff

2 Files Affected:

  • (modified) clang/lib/Basic/Targets/RISCV.cpp (+2)
  • (modified) clang/test/Preprocessor/riscv-target-features.c (+8)
diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp
index a6d4af2b88111a..718d94f2d2621e 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -139,6 +139,7 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
   unsigned MinVLen = ISAInfo->getMinVLen();
   unsigned MaxELen = ISAInfo->getMaxELen();
   unsigned MaxELenFp = ISAInfo->getMaxELenFp();
+  std::string ArchString = "\"" + ISAInfo->toString() + "\"";
   if (CodeModel == "default")
     CodeModel = "small";
 
@@ -222,6 +223,7 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
     else
       Builder.defineMacro("__riscv_32e");
   }
+  Builder.defineMacro("__riscv_cmdline_arch_string", ArchString);
 }
 
 static constexpr Builtin::Info BuiltinInfo[] = {
diff --git a/clang/test/Preprocessor/riscv-target-features.c b/clang/test/Preprocessor/riscv-target-features.c
index 1a15be1c6e4dc1..6023abe2665c2a 100644
--- a/clang/test/Preprocessor/riscv-target-features.c
+++ b/clang/test/Preprocessor/riscv-target-features.c
@@ -185,8 +185,10 @@
 // RUN: %clang --target=riscv64-unknown-linux-gnu \
 // RUN:   -march=rv64ia -E -dM %s \
 // RUN:   -o - | FileCheck --check-prefix=CHECK-A-EXT %s
+// CHECK-A-EXT: __INTPTR_WIDTH__ [[WIDTH:[1-9]+]]
 // CHECK-A-EXT: __riscv_a 2001000{{$}}
 // CHECK-A-EXT: __riscv_atomic 1
+// CHECK-A-EXT: __riscv_cmdline_arch_string "rv[[WIDTH]]i2p1_a2p1"
 
 // RUN: %clang --target=riscv32-unknown-linux-gnu \
 // RUN:   -march=rv32ic -E -dM %s \
@@ -203,6 +205,8 @@
 // RUN: %clang --target=riscv64-unknown-linux-gnu \
 // RUN:   -march=rv64ifd -E -dM %s \
 // RUN:   -o - | FileCheck --check-prefix=CHECK-D-EXT %s
+// CHECK-D-EXT: __INTPTR_WIDTH__ [[WIDTH:[1-9]+]]
+// CHECK-D-EXT: __riscv_cmdline_arch_string "rv[[WIDTH]]i2p1_f2p2_d2p2_zicsr2p0"
 // CHECK-D-EXT: __riscv_d 2002000{{$}}
 // CHECK-D-EXT: __riscv_fdiv 1
 // CHECK-D-EXT: __riscv_flen 64
@@ -217,6 +221,8 @@
 // CHECK-RV32E: __riscv_32e 1
 // CHECK-RV64E: __riscv_64e 1
 // CHECK-E-EXT: __riscv_abi_rve 1
+// CHECK-RV32E: __riscv_cmdline_arch_string "rv32e2p0"
+// CHECK-RV64E: __riscv_cmdline_arch_string "rv64e2p0"
 // CHECK-E-EXT: __riscv_e 2000000{{$}}
 
 // RUN: %clang --target=riscv32-unknown-linux-gnu \
@@ -225,6 +231,8 @@
 // RUN: %clang --target=riscv64-unknown-linux-gnu \
 // RUN:   -march=rv64if -E -dM %s \
 // RUN:   -o - | FileCheck --check-prefix=CHECK-F-EXT %s
+// CHECK-F-EXT: __INTPTR_WIDTH__ [[WIDTH:[1-9]+]]
+// CHECK-F-EXT: __riscv_cmdline_arch_string "rv[[WIDTH]]i2p1_f2p2_zicsr2p0"
 // CHECK-F-EXT: __riscv_f 2002000{{$}}
 // CHECK-F-EXT: __riscv_fdiv 1
 // CHECK-F-EXT: __riscv_flen 32

@nemanjai nemanjai changed the title [RISCV] Emit arch string macro to facilitate ASM programming [DRAFT][RISCV] Emit arch string macro to facilitate ASM programming Mar 13, 2024
@nemanjai
Copy link
Member Author

This is a suggestion born out of a request from a user to have a way to emit the ISA attributes into objects produced from pre-processed asm files. Perhaps there is already a method to do this that I'm not aware of, but if not, I think this is a convenient and lightweight way to do so.

@wangpc-pp
Copy link
Contributor

I think we will add attributes automatically?

~/workspace# cat a.S
        .globl  foo 
        .p2align        1
        .type   foo,@function
foo:
   ret
~/workspace# clang -march=rv64gcv -c a.S
~/workspace# llvm-readobj -A a.o
File: a.o
Format: elf64-littleriscv
Arch: riscv64
AddressSize: 64bit
LoadName: <Not found>
BuildAttributes {
  FormatVersion: 0x41
  Section 1 {
    SectionLength: 157
    Vendor: riscv
    Tag: Tag_File (0x1)
    Size: 147
    FileAttributes {
      Attribute {
        Tag: 5
        TagName: arch
        Value: rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_v1p0_zicsr2p0_zifencei2p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0                                                                            
      }
    }
  }
}

@jrtc27
Copy link
Collaborator

jrtc27 commented Mar 14, 2024

Attributes are added automatically, unless you override it with your own .attribute arch.

@nemanjai
Copy link
Member Author

Ah, I missed the fact that there is an option -riscv-add-build-attributes that the clang driver passes when invoking cc1as. Perhaps that option should default to true? Thanks and sorry for the noise.

@nemanjai nemanjai closed this Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:RISC-V clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants