pointer arithmetic with address space attribute not working

Hi,

I have a case that for pointer with address space attribute, pointer
arithmetic is not work.

For example,
-bash-4.4$ cat test.c
#define __user __attribute__((address_space(1)))
void __user * test(void __user *arg) {
#ifdef ADD
  return arg + 4;
#else
  return arg;
#endif
}
-bash-4.4$

-bash-4.4$ clang -g -c test.c
-bash-4.4$ clang -g -c -DADD test.c
clang-10: ../lib/IR/Instructions.cpp:2749: static llvm::CastInst
*llvm::CastInst::Create(Instruction::CastOps, llvm::Value *,
llvm::Type *, const llvm::Twine
&, llvm::Instruction *): Assertion `castIsValid(op, S, Ty) && "Invalid
cast!"' failed.
Stack dump:
0. Program arguments:
/data/users/yhs/work/llvm-project/llvm/build/install/bin/clang-10 -cc1
-triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disa
ble-free -main-file-name test.c -mrelocation-model static
-mthread-model posix -mframe-pointer=all -fmath-errno -masm-verbose
-mconstructor-aliases -munwind-t
ables -fuse-init-array -target-cpu x86-64 -dwarf-column-info
-debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb
-resource-dir /data/users/yhs/work
/llvm-project/llvm/build/install/lib/clang/10.0.0 -D ADD
-internal-isystem /usr/local/include -internal-isystem
/data/users/yhs/work/llvm-project/llvm/build/i
nstall/lib/clang/10.0.0/include -internal-externc-isystem /include
-internal-externc-isystem /usr/include -fdebug-compilation-dir
/home/yhs/work/tests/user -f
error-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-faddrsig -o test.o -x c test.c
1. <eof> parser at end of file
2. test.c:2:15: LLVM IR generation of declaration 'test'
3. test.c:2:15: Generating code for declaration 'test'
#0 0x00000000017067bf llvm::sys::PrintStackTrace(llvm::raw_ostream&)
(/data/users/yhs/work/llvm-project/llvm/build/install/bin/clang-10+0x17067bf)
#1 0x0000000001704a50 llvm::sys::RunSignalHandlers()
(/data/users/yhs/work/llvm-project/llvm/build/install/bin/clang-10+0x1704a50)
#2 0x0000000001706d28 SignalHandler(int)
(/data/users/yhs/work/llvm-project/llvm/build/install/bin/clang-10+0x1706d28)
#3 0x00007fed0c6c3b10 __restore_rt
(/usr/local/fbcode/platform007/lib/libpthread.so.0+0x12b10)
#4 0x00007fed0b81214f raise
(/usr/local/fbcode/platform007/lib/libc.so.6+0x3714f)
#5 0x00007fed0b7fb935 abort
(/usr/local/fbcode/platform007/lib/libc.so.6+0x20935)
#6 0x00007fed0b80a107 __assert_fail_base
(/usr/local/fbcode/platform007/lib/libc.so.6+0x2f107)
#7 0x00007fed0b80a173 (/usr/local/fbcode/platform007/lib/libc.so.6+0x2f173)
#8 0x00000000011b9381
llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*,
llvm::Type*, llvm::Twine const&, llvm::Instruction*) (/data/users/yhs/
work/llvm-project/llvm/build/install/bin/clang-10+0x11b9381)
#9 0x0000000001b5177f
emitPointerArithmetic(clang::CodeGen::CodeGenFunction&, (anonymous
namespace)::BinOpInfo const&, bool) (/data/users/yhs/work/llvm-proje
ct/llvm/build/install/bin/clang-10+0x1b5177f)
#10 0x0000000001b42b86 clang::StmtVisitorBase<std::add_pointer,
(anonymous namespace)::ScalarExprEmitter,
llvm::Value*>::Visit(clang::Stmt*) (/data/users/yhs/
work/llvm-project/llvm/build/install/bin/clang-10+0x1b42b86)
#11 0x0000000001b3a245
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*,
bool) (/data/users/yhs/work/llvm-project/llvm/build/install/bin/cla
ng-10+0x1b3a245)
#12 0x00000000019226c8
clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt
const&) (/data/users/yhs/work/llvm-project/llvm/build/install/bin/cla
ng-10+0x19226c8)

For pointer arithmetic failure, the clang tries to create a regular
pointer and failed since non-None address space is specified in the
type.

Is this a bug? Or this is the expected behavior? I am asking since
linux kernel has a lot of codes which did pointer arithmetic where
pointers have address space attributes. (currently those address
spaces are sparse tool checking only.)

Thanks,

Yonghong

What clang version is that?
I tried to reproduce it but failed: https://godbolt.org/z/AM1tPp

I forgot the -DADD flag earlier but the result did not change: https://godbolt.org/z/NPcn22

The latest trunk. I am using llvm mono repo. The top two commits are:
b1209d74e3426e7aaa6e5f657d8b2a7554218767 (HEAD -> master,
origin/master, origin/HEAD) gn build: Merge 33a745e6fe7
33a745e6fe7e81d3793f7831d2832aa0785ef327 [clang][clang-scan-deps] Add
support for extracting full module dependencies.

This is my cmake flag, which has assertion turned on. I am tested on
linux platform.
cmake .. -DCMAKE_BUILD_TYPE=Release -G Ninja \
    -DLLVM_ENABLE_PROJECTS="clang" \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
    -DLLVM_ENABLE_ASSERTIONS=ON \
    -DCMAKE_INSTALL_PREFIX=$PWD/install

Thanks for the detailed info!

The problem was that I tried without assertions and didn't inspect the IR closely as it contained this,
for now, illegal instruction: %6 = bitcast i8* %5 to i8 addrspace(1)*

This is clearly a clang error.
I suggest opening a bug report, or sending the reproducer to cfe-dev@llvm.lists.org so someone will take a look that has more clang experience.

@Matt Do you want to take this or ping the right person?

Cheers,
  Johannes

Thanks for the detailed info!

The problem was that I tried without assertions and didn't inspect the IR closely as it contained this,
for now, illegal instruction: %6 = bitcast i8* %5 to i8 addrspace(1)*

This is clearly a clang error.
I suggest opening a bug report, or sending the reproducer to cfe-dev@llvm.lists.org so someone will take a look that has more clang experience.

Thanks! I just verified that without assertion, indeed compilation is
successful.

@Matt Do you want to take this or ping the right person?

If tomorrow morning, nobody claimed this bug I will file a bug :slight_smile:
Thanks for fast response!

I just posted this which fixes this problem: https://reviews.llvm.org/D69666

This is specifically with void typed pointers, if you use char* it works already

-Matt

>
>>
>> Thanks for the detailed info!
>>
>> The problem was that I tried without assertions and didn't inspect the IR closely as it contained this,
>> for now, illegal instruction: %6 = bitcast i8* %5 to i8 addrspace(1)*
>>
>> This is clearly a clang error.
>> I suggest opening a bug report, or sending the reproducer to cfe-dev@llvm.lists.org so someone will take a look that has more clang experience.
>
> Thanks! I just verified that without assertion, indeed compilation is
> successful.
>
>>
>> @Matt Do you want to take this or ping the right person?
>
> If tomorrow morning, nobody claimed this bug I will file a bug :slight_smile:
> Thanks for fast response!

I just posted this which fixes this problem: https://reviews.llvm.org/D69666

Matt, thanks for quick fixing!