Hi,I‘m beginner to clang, I want to add some memory and support its compilation,just like cuda
“__constant__” “__global__”.
I have researched some test cases like clang/test/CodeGenCUDA/address-spaces.cu:
// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple nvptx-unknown-unknown | FileCheck %s
// RUN: %clang_cc1 -emit-llvm %s -o - -fcuda-is-device -triple amdgcn | FileCheck %s
// Verifies Clang emits correct address spaces and addrspacecast instructions
// for CUDA code.
#include "Inputs/cuda.h"
// CHECK: @i ={{.*}} addrspace(1) externally_initialized global
__device__ int i;
// CHECK: @j ={{.*}} addrspace(4) externally_initialized global
__constant__ int j;
// CHECK: @k ={{.*}} addrspace(3) global
__shared__ int k;
struct MyStruct {
int data1;
int data2;
};
// CHECK: @_ZZ5func0vE1a = internal addrspace(3) global %struct.MyStruct undef
// CHECK: @_ZZ5func1vE1a = internal addrspace(3) global float undef
// CHECK: @_ZZ5func2vE1a = internal addrspace(3) global [256 x float] undef
// CHECK: @_ZZ5func3vE1a = internal addrspace(3) global float undef
// CHECK: @_ZZ5func4vE1a = internal addrspace(3) global float undef
// CHECK: @b ={{.*}} addrspace(3) global float undef
__device__ void foo() {
// CHECK: load i32, ptr addrspacecast (ptr addrspace(1) @i to ptr)
i++;
// CHECK: load i32, ptr addrspacecast (ptr addrspace(4) @j to ptr)
j++;
// CHECK: load i32, ptr addrspacecast (ptr addrspace(3) @k to ptr)
k++;
__shared__ int lk;
// CHECK: load i32, ptr addrspacecast (ptr addrspace(3) @_ZZ3foovE2lk to ptr)
lk++;
}
some definition in Inputs/cuda.h:
/* Minimal declarations for CUDA support. Testing purposes only. */
#include <stddef.h>
#if __HIP__ || __CUDA__
#define __constant__ __attribute__((constant))
#define __device__ __attribute__((device))
#define __global__ __attribute__((global))
#define __host__ __attribute__((host))
#define __shared__ __attribute__((shared))
#if __HIP__
#define __managed__ __attribute__((managed))
#endif
#define __launch_bounds__(...) __attribute__((launch_bounds(__VA_ARGS__)))
#else
#define __constant__
#define __device__
#define __global__
#define __host__
#define __shared__
#define __managed__
#define __launch_bounds__(...)
#endif
output LLVM IR:
1 ; ModuleID = 'sample.cu'
2 source_filename = "sample.cu"
3 target datalayout = "e-p:32:32-i64:64-i128:128-v16:16-v32:32-n16:32:64"
4 target triple = "nvptx-unknown-unknown"
5
6 @i = addrspace(1) externally_initialized global i32 0, align 4
7 @j = addrspace(4) externally_initialized global i32 0, align 4
8 @k = addrspace(3) global i32 undef, align 4
9 @_ZZ3foovE2lk = internal addrspace(3) global i32 undef, align 4
10 @llvm.compiler.used = appending global [2 x ptr] [ptr addrspacecast (ptr addrspace(1) @i to ptr), ptr addrspacecast (ptr addrspace(4) @j to ptr)], section "llvm.metadata"
I tried imitating Cuda and made some attempts,
6 #define SIZE 10
7
8 #define __wBuff__ __attribute__((wBuff))
9
10 int aaa[SIZE]={0};
11 int bbb[SIZE]={1};
12 __wBuff__ int ddd[SIZE];
13
clang have a warning and output LLVM IR has no ‘addrspace’ ,
test.c:12:1: warning: unknown attribute 'wBuff' ignored [-Wunknown-attributes]
__wBuff__ int ddd[SIZE];
^~~~~~~~~
test.c:8:34: note: expanded from macro '__wBuff__'
#define __wBuff__ __attribute__((wBuff))
^~~~~
1 warning generated.
So how should I define ’wBuff‘ to generate a normal LLVM IR ?Are there any documents or cases that can be referenced?
I would greatly appreciate any help from you.