Avimitin wrote:

I encountered a clang compiler crash (segmentation fault) when attempting to 
compile the `utf8_count.c` benchmark from the `rvv-bench` project 
([https://github.com/camel-cdr/rvv-bench/blob/main/bench/utf8\_count.c](https://github.com/camel-cdr/rvv-bench/blob/main/bench/utf8_count.c))
 using clang build from this branch. The crash occurs during the 'RISC-V 
DAG-\>DAG Pattern Instruction Selection' pass.

**Steps to Reproduce:**

1.  Obtain the `utf8_count.c` file from the `rvv-bench` project (or use the 
minimal C code provided below).

2.  Compile the C code with the following command:

    ```bash
    riscv32-none-elf-clang -mabi=ilp32f 
-march=rv32imafc_xsfmm128t_zve32f_zvl2048b -mno-relax -static -nostartfiles 
-mcmodel=medany -fvisibility=hidden -fno-PIC -g -O3 -fno-rtti -fno-exceptions 
-fno-threadsafe-statics -c utf8_count.c -o utf8_count.o
    ```

**Error Log:**

<details>
<summary>Compiler Error Output</summary>

```console
$ riscv32-none-elf-clang -mabi=ilp32f 
-march=rv32imafc_xsfmm128t_zve32f_zvl2048b -mno-relax -static -nostartfiles 
-mcmodel=medany -fvisibility=hidden -fno-PIC -g -O3 -fno-rtti -fno-exceptions 
-fno-threadsafe-statics -c test.c -o test.o
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: 
/nix/store/4b17xsrdcyd2bnm91pdclfw2wz3g0ikg-clang-21.0.0-unstable-2025-03-23/bin/clang
 @/tmp/nix-shell.VojIba/cc-params.249Z4g
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module 'utf8_count.c'.
4.      Running pass 'RISC-V DAG->DAG Pattern Instruction Selection' on 
function '@utf8_count_SWAR_popc_bithack_autovec'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH 
or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM.so.21.0git      0x00007fffea78aa0c 
llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 60
1  libLLVM.so.21.0git      0x00007fffea788774 
llvm::sys::CleanupOnSignal(unsigned long) + 148
2  libLLVM.so.21.0git      0x00007fffea668a88
3  libc.so.6               0x00007fffe8e40f30
4  libLLVM.so.21.0git      0x00007fffea9f4454 
llvm::ScalableVectorType::get(llvm::Type*, unsigned int) + 20
5  libLLVM.so.21.0git      0x00007fffeb0d2c50 
llvm::EVT::getExtendedVectorVT(llvm::LLVMContext&, llvm::EVT, 
llvm::ElementCount) + 48
6  libLLVM.so.21.0git      0x00007fffeb35a250
7  libLLVM.so.21.0git      0x00007fffeb35c008
8  libLLVM.so.21.0git      0x00007fffeb2fd970
9  libLLVM.so.21.0git      0x00007fffeb2fe3a9 
llvm::SelectionDAG::LegalizeTypes() + 1321
10 libLLVM.so.21.0git      0x00007fffeb47af5f 
llvm::SelectionDAGISel::CodeGenAndEmitDAG() + 271
11 libLLVM.so.21.0git      0x00007fffeb47dc5a 
llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 5274
12 libLLVM.so.21.0git      0x00007fffeb47fb99 
llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 217
13 libLLVM.so.21.0git      0x00007fffeb46e8e9 
llvm::SelectionDAGISelLegacy::runOnMachineFunction(llvm::MachineFunction&) + 409
14 libLLVM.so.21.0git      0x00007fffead5e853
15 libLLVM.so.21.0git      0x00007fffea972d89 
llvm::FPPassManager::runOnFunction(llvm::Function&) + 1705
16 libLLVM.so.21.0git      0x00007fffea972f3c 
llvm::FPPassManager::runOnModule(llvm::Module&) + 44
17 libLLVM.so.21.0git      0x00007fffea971f69 
llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1081
18 libclang-cpp.so.21.0git 0x00007ffff5cc47f4 
clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, 
llvm::StringRef, llvm::Module*, clang::BackendAction, 
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, 
std::unique_ptr<llvm::raw_pwrite_stream, 
std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) + 2772
19 libclang-cpp.so.21.0git 0x00007ffff60764bc 
clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 1692
20 libclang-cpp.so.21.0git 0x00007ffff4616eec clang::ParseAST(clang::Sema&, 
bool, bool) + 1212
21 libclang-cpp.so.21.0git 0x00007ffff6a79928 clang::FrontendAction::Execute() 
+ 40
22 libclang-cpp.so.21.0git 0x00007ffff69eec13 
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1475
23 libclang-cpp.so.21.0git 0x00007ffff6b1b11b 
clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 539
24 clang                   0x000000000041670a cc1_main(llvm::ArrayRef<char 
const*>, char const*, void*) + 7306
25 clang                   0x000000000040e8f3
26 libclang-cpp.so.21.0git 0x00007ffff65d9a99
27 libLLVM.so.21.0git      0x00007fffea668e93 
llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 35
28 libclang-cpp.so.21.0git 0x00007ffff65da545
29 libclang-cpp.so.21.0git 0x00007ffff659966c 
clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, 
clang::driver::Command const*&, bool) const + 172
30 libclang-cpp.so.21.0git 0x00007ffff659a652 
clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, 
llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) 
const + 146
31 libclang-cpp.so.21.0git 0x00007ffff65b017c 
clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, 
llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) + 364
32 clang                   0x00000000004132f4 clang_main(int, char**, 
llvm::ToolContext const&) + 8308
33 clang                   0x000000000040e2b4 main + 100
34 libc.so.6               0x00007fffe8e2a1fe
35 libc.so.6               0x00007fffe8e2a2b9 __libc_start_main + 137
36 clang                   0x000000000040e315 _start + 37
clang: error: clang frontend command failed with exit code 139 (use -v to see 
invocation)
clang version 21.0.0git
Target: riscv32-unknown-none-elf
Thread model: posix
InstalledDir: 
/nix/store/4b17xsrdcyd2bnm91pdclfw2wz3g0ikg-clang-21.0.0-unstable-2025-03-23/bin
clang: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/nix-shell.VojIba/utf8_count-cefc8d.c
clang: note: diagnostic msg: /tmp/nix-shell.VojIba/utf8_count-cefc8d.sh
clang: note: diagnostic msg:

********************
```

</details>

**Minimal Reproducible C Code:**

The crash appears to be triggered by the `utf8_count_SWAR_popc_bithack_autovec` 
function.

<details>
<summary>Reproduce C code (test.c)</summary>

```c
#include <limits.h>
#include <stdint.h>
#include <stddef.h>
#include <float.h>

typedef uint32_t ux;

static inline int upopcnt(ux x)
{

 x -= (x >> 1) & (-(ux)1/3);

 x = (x & (-(ux)1/15*3)) + ((x >> 2) & (-(ux)1/15*3));

 x = (x + (x >> 4)) & (-(ux)1/255*15);
 ({__asm volatile("" : "+r"(x) : "r"(x) : "memory");});


 x += (x >> 8);
 x += (x >> 16);
                     ;
 return x & 127;
}

size_t utf8_count_SWAR_popc_bithack_autovec(char const *str, size_t len) {
  ux const __attribute__((__may_alias__)) * u;
  size_t count = 0, tail = 0;
  uint8_t const *u8 = (uint8_t const *)str;
  if (len < sizeof *u) {
    tail = len;
    goto skip;
  }
  tail = sizeof *u - (uintptr_t)str % sizeof *u;
  len -= tail;
  while (tail--)
    count += (*u8++ & 0xC0) != 0x80, (void)0;
  u = (ux const *)u8;
  tail = len % sizeof *u;
  for (len /= sizeof *u; len--; ++u) {
    ux b1 = ~*u & (ux)0x80808080;
    ux b2 = *u & (ux)0x40404040;
    count += upopcnt((b1 >> 1) | b2);
    (void)0;
  }
  u8 = (uint8_t const *)u;
skip:
  while (tail--)
    count += (*u8++ & 0xC0) != 0x80, (void)0;
  return count;
}

```

</details>

**Compiler Flags Used:**

```text
-mabi=ilp32f -march=rv32imafc_xsfmm128t_zve32f_zvl2048b -mno-relax -static 
-nostartfiles -mcmodel=medany -fvisibility=hidden -fno-PIC -g -O3 -fno-rtti 
-fno-exceptions -fno-threadsafe-statics
```

https://github.com/llvm/llvm-project/pull/133031
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to