#include "device_launch_parameters.h"
#include <iostream>
#include <stdio.h>
#include <cuda_runtime.h>
#include <time.h>
using namespace std;
#define eps 1e-5


//在原来的情况下，读可以合并，因为可以索引到连续的内存空间（例如行优先），但是写的时候只能跳跃访存写了。
//现在利用shared把一个tile的读进shared进行转置写，就可以高效了
__global__ void mat_transpose(const float *a, float *b, int n, int m){
    const int TIlE_WIDTH = 8;
    __shared__ float temp[TIlE_WIDTH][TIlE_WIDTH];

    int bx = blockIdx.x, by = blockIdx.y;
    int tx = threadIdx.x, ty = threadIdx.y;
    
    int i = TIlE_WIDTH * bx + tx;
    int j = TIlE_WIDTH * by + ty;
    int idxa = j * n + i;
    int idxb = i * n + j;

    temp[ty][tx] = a[idxa];
    __syncthreads();

    b[idxb] = temp[ty][tx];

    // if(i < n and j < m){
    //     b[idxb] = a[idxa];
    // }
}

void check_mat_transpose(const float *a, const float *b, int n, int m){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int idxa = i * m + j;
            int idxb = j * m + i;
            //cout<<i<<' '<<j<<' '<<a[idxa]<<' '<<b[idxb]<<endl;
            if(fabs(a[idxa] - b[idxb]) > eps){
                printf("Not equal !!\n");
                exit(1);
            }
        }
    }
    printf("Check matmul success!!!\n");
}


void run_matmul(){
    int n = 1<<7;
    int m = 1<<7;
    int total = n * m;
    size_t size = (total) * sizeof(float);

    float *ha = (float*)malloc(size);
    float *hb = (float*)malloc(size);
    float *hc = (float*)malloc(size);

    float *da = NULL, *db = NULL, *dc = NULL;
    cudaMalloc((void**)&da, size);
    cudaMalloc((void**)&db, size);
    cudaMalloc((void**)&dc, size);
    
    for(int i = 0; i < total; i++){
        ha[i] = rand() * 1.0 / (RAND_MAX);
        //hb[i] = rand() * 1.0 / (RAND_MAX);
        //cout<<ha[i]<<' '<<hb[i]<<endl;
    }
    // for(int i = 0; i < total; i++){
    //     cout<<ha[i]<< ' ';
    // }
    // cout<<endl;
    // for(int i = 0; i < total; i++){
    //     cout<<hb[i]<< ' ';
    // }
    // cout<<endl;
    

    cudaMemcpy(da, ha, size, cudaMemcpyHostToDevice);
    //cudaMemcpy(db, hb, size, cudaMemcpyHostToDevice);

    //int threadPerBlock = 512;
    //int blockPerGrid = (total + threadPerBlock - 1) / threadPerBlock;
    //clock_t st = clock();
    dim3 threadPerBlock(8,8);
    dim3 blockPerGrid((n+threadPerBlock.x-1)/threadPerBlock.x, (m+threadPerBlock.y-1)/threadPerBlock.y);
    mat_transpose<<<blockPerGrid, threadPerBlock>>>(da, db, n, m);    
    //matmul<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);    

    // dim3 threadPerBlock(512);
    // dim3 blockPerGrid((threadPerBlock.x + total - 1) / threadPerBlock.x);
    // matadd_1d<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);
    // dim3 threadPerBlock(512, 1);
    // dim3 blockPerGrid((n+threadPerBlock.x-1)/threadPerBlock.x, (m+threadPerBlock.y-1)/threadPerBlock.y);
    // matadd_2d<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);


    //clock_t ed = clock();
    //cout<<"time used: "<<ed-st<<endl;
    cudaDeviceSynchronize();
    cudaMemcpy(hb, db, size, cudaMemcpyDeviceToHost);
    //check_matadd(ha, hb, hc, n, m);
    check_mat_transpose(ha, hb, n, m);


    cudaFree(da);
    cudaFree(db);
    cudaFree(dc);

    free(ha);
    free(hb);
    free(hc);
}


int main(){
    //run_matmul_partition();
    run_matmul();
    return 0;
}
	.file	"tmpxft_00332b39_00000000-6_transpose.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3640:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3640:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"Not equal !!\n"
.LC3:
	.string	"Check matmul success!!!\n"
	.text
	.globl	_Z19check_mat_transposePKfS0_ii
	.type	_Z19check_mat_transposePKfS0_ii, @function
_Z19check_mat_transposePKfS0_ii:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movslq	%ecx, %r10
	movsd	.LC1(%rip), %xmm2
	movl	%edx, %r11d
	movq	%r10, %rax
	xorl	%r8d, %r8d
	salq	$2, %r10
	xorl	%r9d, %r9d
	movss	.LC0(%rip), %xmm1
.L3:
	cmpl	%r11d, %r9d
	jge	.L7
	movslq	%r8d, %rdx
	movq	%rsi, %rcx
	leaq	(%rdi,%rdx,4), %rbx
	xorl	%edx, %edx
.L4:
	cmpl	%edx, %eax
	jle	.L11
	movss	(%rbx,%rdx,4), %xmm0
	subss	(%rcx), %xmm0
	incq	%rdx
	addq	%r10, %rcx
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm2, %xmm0
	jbe	.L4
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L11:
	incl	%r9d
	addq	$4, %rsi
	addl	%eax, %r8d
	jmp	.L3
.L7:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE3635:
	.size	_Z19check_mat_transposePKfS0_ii, .-_Z19check_mat_transposePKfS0_ii
	.globl	_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii
	.type	_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii, @function
_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii:
.LFB3662:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L12
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z13mat_transposePKfPfii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L12:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3662:
	.size	_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii, .-_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii
	.globl	_Z13mat_transposePKfPfii
	.type	_Z13mat_transposePKfPfii, @function
_Z13mat_transposePKfPfii:
.LFB3663:
	.cfi_startproc
	endbr64
	jmp	_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii
	.cfi_endproc
.LFE3663:
	.size	_Z13mat_transposePKfPfii, .-_Z13mat_transposePKfPfii
	.globl	_Z10run_matmulv
	.type	_Z10run_matmulv, @function
_Z10run_matmulv:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$65536, %edi
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$64, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$65536, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	leaq	8(%rsp), %rdi
	movl	$65536, %esi
	movq	%rax, %rbp
	xorl	%eax, %eax
	movq	%rax, 8(%rsp)
	movq	%rax, 16(%rsp)
	movq	%rax, 24(%rsp)
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
.L18:
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC4(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r12,4)
	incq	%r12
	cmpq	$16384, %r12
	jne	.L18
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movl	$65536, %edx
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	movabsq	$34359738376, %rdx
	movl	$1, %esi
	movabsq	$68719476752, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	movl	$128, %ecx
	movl	$128, %edx
	call	_Z38__device_stub__Z13mat_transposePKfPfiiPKfPfii
.L19:
	call	cudaDeviceSynchronize@PLT
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$65536, %edx
	call	cudaMemcpy@PLT
	movl	$128, %ecx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	movl	$128, %edx
	call	_Z19check_mat_transposePKfS0_ii
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$64, %rsp
	.cfi_def_cfa_offset 32
	movq	%rbp, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE3636:
	.size	_Z10run_matmulv, .-_Z10run_matmulv
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3637:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z10run_matmulv
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3637:
	.size	main, .-main
	.section	.rodata.str1.1
.LC5:
	.string	"_Z13mat_transposePKfPfii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3665:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC5(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13mat_transposePKfPfii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3665:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC0:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	-1998362383
	.long	1055193269
	.align 8
.LC4:
	.long	-4194304
	.long	1105199103
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <iostream>
#include <stdio.h>
#include <hip/hip_runtime.h>
#include <time.h>
using namespace std;
#define eps 1e-5


//在原来的情况下，读可以合并，因为可以索引到连续的内存空间（例如行优先），但是写的时候只能跳跃访存写了。
//现在利用shared把一个tile的读进shared进行转置写，就可以高效了
__global__ void mat_transpose(const float *a, float *b, int n, int m){
    const int TIlE_WIDTH = 8;
    __shared__ float temp[TIlE_WIDTH][TIlE_WIDTH];

    int bx = blockIdx.x, by = blockIdx.y;
    int tx = threadIdx.x, ty = threadIdx.y;
    
    int i = TIlE_WIDTH * bx + tx;
    int j = TIlE_WIDTH * by + ty;
    int idxa = j * n + i;
    int idxb = i * n + j;

    temp[ty][tx] = a[idxa];
    __syncthreads();

    b[idxb] = temp[ty][tx];

    // if(i < n and j < m){
    //     b[idxb] = a[idxa];
    // }
}

void check_mat_transpose(const float *a, const float *b, int n, int m){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int idxa = i * m + j;
            int idxb = j * m + i;
            //cout<<i<<' '<<j<<' '<<a[idxa]<<' '<<b[idxb]<<endl;
            if(fabs(a[idxa] - b[idxb]) > eps){
                printf("Not equal !!\n");
                exit(1);
            }
        }
    }
    printf("Check matmul success!!!\n");
}


void run_matmul(){
    int n = 1<<7;
    int m = 1<<7;
    int total = n * m;
    size_t size = (total) * sizeof(float);

    float *ha = (float*)malloc(size);
    float *hb = (float*)malloc(size);
    float *hc = (float*)malloc(size);

    float *da = NULL, *db = NULL, *dc = NULL;
    hipMalloc((void**)&da, size);
    hipMalloc((void**)&db, size);
    hipMalloc((void**)&dc, size);
    
    for(int i = 0; i < total; i++){
        ha[i] = rand() * 1.0 / (RAND_MAX);
        //hb[i] = rand() * 1.0 / (RAND_MAX);
        //cout<<ha[i]<<' '<<hb[i]<<endl;
    }
    // for(int i = 0; i < total; i++){
    //     cout<<ha[i]<< ' ';
    // }
    // cout<<endl;
    // for(int i = 0; i < total; i++){
    //     cout<<hb[i]<< ' ';
    // }
    // cout<<endl;
    

    hipMemcpy(da, ha, size, hipMemcpyHostToDevice);
    //cudaMemcpy(db, hb, size, cudaMemcpyHostToDevice);

    //int threadPerBlock = 512;
    //int blockPerGrid = (total + threadPerBlock - 1) / threadPerBlock;
    //clock_t st = clock();
    dim3 threadPerBlock(8,8);
    dim3 blockPerGrid((n+threadPerBlock.x-1)/threadPerBlock.x, (m+threadPerBlock.y-1)/threadPerBlock.y);
    mat_transpose<<<blockPerGrid, threadPerBlock>>>(da, db, n, m);    
    //matmul<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);    

    // dim3 threadPerBlock(512);
    // dim3 blockPerGrid((threadPerBlock.x + total - 1) / threadPerBlock.x);
    // matadd_1d<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);
    // dim3 threadPerBlock(512, 1);
    // dim3 blockPerGrid((n+threadPerBlock.x-1)/threadPerBlock.x, (m+threadPerBlock.y-1)/threadPerBlock.y);
    // matadd_2d<<<blockPerGrid, threadPerBlock>>>(da, db, dc, n, m);


    //clock_t ed = clock();
    //cout<<"time used: "<<ed-st<<endl;
    hipDeviceSynchronize();
    hipMemcpy(hb, db, size, hipMemcpyDeviceToHost);
    //check_matadd(ha, hb, hc, n, m);
    check_mat_transpose(ha, hb, n, m);


    hipFree(da);
    hipFree(db);
    hipFree(dc);

    free(ha);
    free(hb);
    free(hc);
}


int main(){
    //run_matmul_partition();
    run_matmul();
    return 0;
}
	.text
	.file	"transpose.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z28__device_stub__mat_transposePKfPfii # -- Begin function _Z28__device_stub__mat_transposePKfPfii
	.type	_Z28__device_stub__mat_transposePKfPfii,@function
_Z28__device_stub__mat_transposePKfPfii: # @_Z28__device_stub__mat_transposePKfPfii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z13mat_transposePKfPfii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z28__device_stub__mat_transposePKfPfii, .Lfunc_end0-_Z28__device_stub__mat_transposePKfPfii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z19check_mat_transposePKfS0_ii
.LCPI1_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_1:
	.quad	0x3ee4f8b588e368f1              # double 1.0000000000000001E-5
	.text
	.globl	_Z19check_mat_transposePKfS0_ii
	.type	_Z19check_mat_transposePKfS0_ii,@function
_Z19check_mat_transposePKfS0_ii:        # @_Z19check_mat_transposePKfS0_ii
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	testl	%edx, %edx
	jle	.LBB1_7
# %bb.1:                                # %.preheader.lr.ph
	movslq	%ecx, %rax
	movl	%edx, %edx
	movl	%eax, %r8d
	shlq	$2, %rax
	xorl	%r9d, %r9d
	movaps	.LCPI1_0(%rip), %xmm0           # xmm0 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero
	xorl	%r10d, %r10d
.LBB1_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	testl	%ecx, %ecx
	jle	.LBB1_6
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	%r9d, %r11d
	leaq	(%rdi,%r11,4), %r11
	movq	%rsi, %rbx
	xorl	%r14d, %r14d
.LBB1_4:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r11,%r14,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	subss	(%rbx), %xmm2
	andps	%xmm0, %xmm2
	cvtss2sd	%xmm2, %xmm2
	ucomisd	%xmm1, %xmm2
	ja	.LBB1_8
# %bb.5:                                #   in Loop: Header=BB1_4 Depth=2
	incq	%r14
	addq	%rax, %rbx
	cmpq	%r14, %r8
	jne	.LBB1_4
.LBB1_6:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	incq	%r10
	addl	%ecx, %r9d
	addq	$4, %rsi
	cmpq	%rdx, %r10
	jne	.LBB1_2
.LBB1_7:                                # %._crit_edge20
	movl	$.Lstr, %edi
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.LBB1_8:
	.cfi_def_cfa_offset 32
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z19check_mat_transposePKfS0_ii, .Lfunc_end1-_Z19check_mat_transposePKfS0_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10run_matmulv
.LCPI2_0:
	.quad	0x41dfffffffc00000              # double 2147483647
	.text
	.globl	_Z10run_matmulv
	.type	_Z10run_matmulv,@function
_Z10run_matmulv:                        # @_Z10run_matmulv
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$65536, %edi                    # imm = 0x10000
	callq	malloc
	movq	%rax, %rbx
	movl	$65536, %edi                    # imm = 0x10000
	callq	malloc
	movq	%rax, %r14
	xorl	%r13d, %r13d
	leaq	16(%rsp), %rdi
	movq	%r13, (%rdi)
	leaq	8(%rsp), %r15
	movq	%r13, (%r15)
	leaq	24(%rsp), %r12
	movq	%r13, (%r12)
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	movl	$65536, %esi                    # imm = 0x10000
	movq	%r15, %rdi
	callq	hipMalloc
	movl	$65536, %esi                    # imm = 0x10000
	movq	%r12, %rdi
	callq	hipMalloc
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r13,4)
	incq	%r13
	cmpq	$16384, %r13                    # imm = 0x4000
	jne	.LBB2_1
# %bb.2:
	movq	16(%rsp), %rdi
	movl	$65536, %edx                    # imm = 0x10000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$68719476752, %rdi              # imm = 0x1000000010
	movabsq	$34359738376, %rdx              # imm = 0x800000008
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_4
# %bb.3:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	$128, %edx
	movl	$128, %ecx
	callq	_Z28__device_stub__mat_transposePKfPfii
.LBB2_4:
	callq	hipDeviceSynchronize
	movq	8(%rsp), %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movl	$128, %edx
	movl	$128, %ecx
	callq	_Z19check_mat_transposePKfS0_ii
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z10run_matmulv, .Lfunc_end2-_Z10run_matmulv
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	_Z10run_matmulv
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13mat_transposePKfPfii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13mat_transposePKfPfii,@object # @_Z13mat_transposePKfPfii
	.section	.rodata,"a",@progbits
	.globl	_Z13mat_transposePKfPfii
	.p2align	3, 0x0
_Z13mat_transposePKfPfii:
	.quad	_Z28__device_stub__mat_transposePKfPfii
	.size	_Z13mat_transposePKfPfii, 8

	.type	.L__unnamed_1,@object           # @0
	.section	.rodata.str1.1,"aMS",@progbits,1
.L__unnamed_1:
	.asciz	"_Z13mat_transposePKfPfii"
	.size	.L__unnamed_1, 25

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Check matmul success!!!"
	.size	.Lstr, 24

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Not equal !!"
	.size	.Lstr.1, 13

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z28__device_stub__mat_transposePKfPfii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13mat_transposePKfPfii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13mat_transposePKfPfii ; -- Begin function _Z13mat_transposePKfPfii
	.globl	_Z13mat_transposePKfPfii
	.p2align	8
	.type	_Z13mat_transposePKfPfii,@function
_Z13mat_transposePKfPfii:               ; @_Z13mat_transposePKfPfii
; %bb.0:
	s_load_b32 s4, s[0:1], 0x10
	v_and_b32_e32 v4, 0x3ff, v0
	v_bfe_u32 v5, v0, 10, 10
	s_load_b128 s[0:3], s[0:1], 0x0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshl_add_u32 v0, s14, 3, v4
	v_lshl_add_u32 v1, s15, 3, v5
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, v1, s4, v[0:1]
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_u32 v2, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	global_load_b32 v6, v[2:3], off
	v_lshlrev_b32_e32 v2, 2, v4
	v_lshl_add_u32 v4, v5, 5, v2
	v_mad_u64_u32 v[2:3], null, v0, s4, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	s_waitcnt vmcnt(0)
	ds_store_b32 v4, v6
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v4, v4
	s_waitcnt lgkmcnt(0)
	global_store_b32 v[0:1], v4, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13mat_transposePKfPfii
		.amdhsa_group_segment_fixed_size 256
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z13mat_transposePKfPfii, .Lfunc_end0-_Z13mat_transposePKfPfii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 220
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 256 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 256
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13mat_transposePKfPfii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13mat_transposePKfPfii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
