//question:
//does contention for accessing global memory affect read performance?
//for instance, n threads access each of the n rows in a column at the same time.
//would it be faster for thread i to access row i % n first and proceed to i + j % n?

#define TILE_WIDTH 32
#define DIVIDE_ROUND_UP(a, b)((a + b - 1) / b)
#define GET_INDEX(row, column, numcols)(row * numcols + column)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//define matrix type
typedef struct{
  int row_count;
  int column_count;
  int* elements;
} Matrix;


__global__ void multiply_kernel_stupid(const Matrix left, const Matrix right, Matrix result);
Matrix ones(int row_count, int column_count);
Matrix random(int row_count, int column_count);
Matrix multiply(Matrix left, Matrix right);
void print_matrix(Matrix mat);

int main(){
  //make the matrices you want to multiply
  srand(time(NULL));
  Matrix A = random(512, 512);
  Matrix B = random(512, 512);
  Matrix result = multiply(A, B);
  print_matrix(A);
  print_matrix(B);
  print_matrix(result);

}

//global memory
__global__ void multiply_kernel_stupid(const Matrix left, const Matrix right, Matrix result){
  int sum = 0;
  int row_index = blockIdx.y * blockDim.y + threadIdx.y;
  int column_index = blockIdx.x * blockDim.x + threadIdx.x;
  for(int i = 0; i < left.column_count; i++){
    int left_index = row_index * left.column_count  + i;
    int right_index = column_index + i * right.column_count;
    sum += left.elements[left_index] * right.elements[right_index];
  }
  result.elements[row_index * result.row_count + column_index] = sum;

}

//better kernel, shared memory
__global__ void multiply_kernel_smart(const Matrix left, const Matrix right, Matrix result){
  // allocate shared memory
  __shared__ int left_shared[TILE_WIDTH*TILE_WIDTH];
  __shared__ int right_shared[TILE_WIDTH*TILE_WIDTH];

  //get row/col indices
  int result_row_index = threadIdx.y + blockDim.y * blockIdx.y;
  int result_col_index = threadIdx.x + blockDim.x * blockIdx.x;
  //int grid_row = blockIdx.y;
  //int grid_col = blockIdx.x;
  int block_row = threadIdx.y;
  int block_col = threadIdx.x;

  //how many blocks do we need to multiply?
  int num_block_mult = DIVIDE_ROUND_UP(left.column_count, blockDim.x);

  //loop through the tiles that we need to multiply
  for(int i = 0; i < num_block_mult; i++){
    //copy relevant blocks to shared memory, watch out for overflow
    int left_col_index = i * blockDim.x + block_col;
    int& left_row_index = result_row_index; //different name for readability
    int& right_col_index = result_col_index; //different name for readability
    int right_row_index = i * blockDim.x + block_row;
    int shared_array_index = block_col + block_row * blockDim.x;
    int left_index = GET_INDEX(left_row_index, left_col_index, left.column_count);
    int right_index = GET_INDEX(right_row_index, right_col_index, right.column_count);

    if(left_col_index < left.column_count && left_row_index < left.row_count){
      left_shared[shared_array_index] = left.elements[left_index];
    }
    else{
      left_shared[shared_array_index] = 0;
    }

    if(right_col_index < right.column_count && right_row_index < right.row_count){
      right_shared[shared_array_index] = right.elements[right_index];
    }
    else{
      right_shared[shared_array_index] = 0;
    }

    //make sure threads all finish copying to shared memory before doing multiplications
    __syncthreads();

    //do multiplications
    int dot_product = 0;
    for(int k = 0; k < blockDim.x; k++){
      dot_product += left_shared[GET_INDEX(blockIdx.y, k, blockDim.x)] * right_shared[GET_INDEX(k, blockIdx.x, blockDim.x)];
    }

    //make sure threads all finish multiplying before writing resulting block to memory
    __syncthreads();

    //write to global memory
    result.elements[GET_INDEX(result_row_index, result_col_index, result.column_count)] = dot_product;
  }

}

Matrix multiply(Matrix left, Matrix right){
  cudaError_t error;
  //step 1: allocate memory on the kernel for left, right, result
  Matrix left_d, right_d;
  left_d.row_count = left.row_count;
  left_d.column_count = left.column_count;
  size_t left_size = left.row_count * left.column_count * sizeof(int);
  error = cudaMalloc((void**) &left_d.elements, left_size);
  if(error != cudaSuccess){
    printf("error allocating left matrix\n");
    printf("CUDA error: %s\n", cudaGetErrorString(error));
  }


  right_d.row_count = right.row_count;
  right_d.column_count = right.column_count;
  size_t right_size = right.row_count * right.column_count * sizeof(int);
  error = cudaMalloc((void**) &right_d.elements, right_size);
  if(error != cudaSuccess){ printf("error allocating right matrix\n"); }

  //step 2: allocate memory on the host and device for result
  Matrix result, result_d;
  result.row_count = result_d.row_count = left.row_count;
  result.column_count = result_d.column_count = right.column_count;
  size_t result_size = result.row_count * result.column_count * sizeof(int);
  result.elements = (int*) malloc(result_size);
  error = cudaMalloc((void**) &result_d.elements, result_size);
  if(error != cudaSuccess){ printf("error allocating matrix\n"); }

  //step 3: copy left and right to device
  error = cudaMemcpy(left_d.elements, left.elements, left_size, cudaMemcpyHostToDevice);
  if(error != cudaSuccess){ printf("error copying left matrix\n"); }
  error = cudaMemcpy(right_d.elements, right.elements, right_size, cudaMemcpyHostToDevice);
  if(error != cudaSuccess){ printf("error copying right matrix\n"); }

  //step 4: launch kernel



  dim3 block_dims(TILE_WIDTH, TILE_WIDTH);
  dim3 grid_dims(DIVIDE_ROUND_UP(result.column_count, block_dims.x), DIVIDE_ROUND_UP(result.row_count, block_dims.y));
  multiply_kernel_smart <<<grid_dims, block_dims>>> (left_d, right_d, result_d);

  //step 5: copy results back to host
  error = cudaMemcpy(result.elements, result_d.elements, result_size, cudaMemcpyDeviceToHost);
  if(error != cudaSuccess){
  	printf("error copying result matrix\n");
  	printf("CUDA error: %s\n", cudaGetErrorString(error));
  }
  return result;
}

Matrix ones (int row_count, int column_count){
  Matrix result;
  result.row_count = row_count;
  result.column_count = column_count;
  result.elements = (int*) malloc(row_count * column_count * sizeof(int));
  for(int i = 0; i < row_count * column_count; i++){
    result.elements[i] = 1;
  }
  return result;
}

Matrix random (int row_count, int column_count){
  Matrix result;
  result.row_count = row_count;
  result.column_count = column_count;
  result.elements = (int*) malloc(row_count * column_count * sizeof(int));
  for(int i = 0; i < row_count * column_count; i++){
    result.elements[i] = rand() % 100;
  }
  return result;
}

void print_matrix(Matrix mat){
  int num_elements = mat.row_count * mat.column_count;
  for(int i = 0; i < num_elements; i++){
    printf(" %d", mat.elements[i]);
    if(!((i + 1) % mat.column_count)){ printf("\n"); }
  }
}

	.file	"tmpxft_002413a7_00000000-6_cudaMatrixMultiply.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z4onesii
	.type	_Z4onesii, @function
_Z4onesii:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	%edi, %r12d
	imull	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edi, %ebx
	movslq	%r12d, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %rdx
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %r12d
	jle	.L7
	movl	$1, (%rdx,%rax,4)
	incq	%rax
	jmp	.L3
.L7:
	salq	$32, %rbp
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	orq	%rbp, %rax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z4onesii, .-_Z4onesii
	.globl	_Z6randomii
	.type	_Z6randomii, @function
_Z6randomii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$100, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%edi, %r14d
	imull	%esi, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	movslq	%r14d, %rdi
	pushq	%r8
	.cfi_def_cfa_offset 64
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, %r12
.L9:
	cmpl	%r13d, %r14d
	jle	.L12
	call	rand@PLT
	cltd
	idivl	%r15d
	movl	%edx, (%r12,%r13,4)
	incq	%r13
	jmp	.L9
.L12:
	salq	$32, %rbp
	movl	%ebx, %eax
	popq	%rcx
	.cfi_def_cfa_offset 56
	movq	%r12, %rdx
	popq	%rbx
	.cfi_def_cfa_offset 48
	orq	%rbp, %rax
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z6randomii, .-_Z6randomii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	" %d"
.LC1:
	.string	"\n"
	.text
	.globl	_Z12print_matrix6Matrix
	.type	_Z12print_matrix6Matrix, @function
_Z12print_matrix6Matrix:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC0(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rdi, %rbp
	sarq	$32, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	movl	%ebp, %r13d
	imull	%edi, %ebp
.L14:
	cmpl	%ebx, %ebp
	jle	.L18
	movl	(%r12,%rbx,4), %edx
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leal	1(%rbx), %eax
	cltd
	idivl	%r13d
	testl	%edx, %edx
	jne	.L15
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L15:
	incq	%rbx
	jmp	.L14
.L18:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z12print_matrix6Matrix, .-_Z12print_matrix6Matrix
	.globl	_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_
	.type	_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_, @function
_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rcx
	movq	%rdi, 64(%rsp)
	movabsq	$4294967297, %rax
	movq	%rsi, 72(%rsp)
	leaq	16(%rsp), %rdi
	leaq	28(%rsp), %rsi
	movq	%rdx, 80(%rsp)
	movq	%rsp, %rdx
	movq	%rax, 16(%rsp)
	movl	$1, 24(%rsp)
	movq	%rax, 28(%rsp)
	movl	$1, 36(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	pushq	8(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z22multiply_kernel_stupid6MatrixS_S_(%rip), %rdi
	pushq	8(%rsp)
	.cfi_def_cfa_offset 128
	movq	44(%rsp), %rcx
	movl	52(%rsp), %r8d
	movq	32(%rsp), %rsi
	movl	40(%rsp), %edx
	leaq	80(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L19:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_, .-_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_
	.globl	_Z22multiply_kernel_stupid6MatrixS_S_
	.type	_Z22multiply_kernel_stupid6MatrixS_S_, @function
_Z22multiply_kernel_stupid6MatrixS_S_:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$56, %rsp
	.cfi_def_cfa_offset 64
	movq	%rdi, 32(%rsp)
	leaq	32(%rsp), %rdi
	movq	%rsi, 40(%rsp)
	leaq	16(%rsp), %rsi
	movq	%rdx, 16(%rsp)
	movq	%rsp, %rdx
	movq	%rcx, 24(%rsp)
	movq	%r8, (%rsp)
	movq	%r9, 8(%rsp)
	call	_Z51__device_stub__Z22multiply_kernel_stupid6MatrixS_S_RK6MatrixS1_RS_
	addq	$56, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z22multiply_kernel_stupid6MatrixS_S_, .-_Z22multiply_kernel_stupid6MatrixS_S_
	.globl	_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_
	.type	_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_, @function
_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_:
.LFB2058:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rcx
	movq	%rdi, 64(%rsp)
	movabsq	$4294967297, %rax
	movq	%rsi, 72(%rsp)
	leaq	16(%rsp), %rdi
	leaq	28(%rsp), %rsi
	movq	%rdx, 80(%rsp)
	movq	%rsp, %rdx
	movq	%rax, 16(%rsp)
	movl	$1, 24(%rsp)
	movq	%rax, 28(%rsp)
	movl	$1, 36(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L25
	pushq	8(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z21multiply_kernel_smart6MatrixS_S_(%rip), %rdi
	pushq	8(%rsp)
	.cfi_def_cfa_offset 128
	movq	44(%rsp), %rcx
	movl	52(%rsp), %r8d
	movq	32(%rsp), %rsi
	movl	40(%rsp), %edx
	leaq	80(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L25:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2058:
	.size	_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_, .-_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_
	.globl	_Z21multiply_kernel_smart6MatrixS_S_
	.type	_Z21multiply_kernel_smart6MatrixS_S_, @function
_Z21multiply_kernel_smart6MatrixS_S_:
.LFB2059:
	.cfi_startproc
	endbr64
	subq	$56, %rsp
	.cfi_def_cfa_offset 64
	movq	%rdi, 32(%rsp)
	leaq	32(%rsp), %rdi
	movq	%rsi, 40(%rsp)
	leaq	16(%rsp), %rsi
	movq	%rdx, 16(%rsp)
	movq	%rsp, %rdx
	movq	%rcx, 24(%rsp)
	movq	%r8, (%rsp)
	movq	%r9, 8(%rsp)
	call	_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_
	addq	$56, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2059:
	.size	_Z21multiply_kernel_smart6MatrixS_S_, .-_Z21multiply_kernel_smart6MatrixS_S_
	.section	.rodata.str1.1
.LC2:
	.string	"error allocating left matrix\n"
.LC3:
	.string	"CUDA error: %s\n"
.LC4:
	.string	"error allocating right matrix\n"
.LC5:
	.string	"error allocating matrix\n"
.LC6:
	.string	"error copying left matrix\n"
.LC7:
	.string	"error copying right matrix\n"
.LC8:
	.string	"error copying result matrix\n"
	.text
	.globl	_Z8multiply6MatrixS_
	.type	_Z8multiply6MatrixS_, @function
_Z8multiply6MatrixS_:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rcx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	sarq	$32, %r12
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	sarq	$32, %rbp
	movq	%rdi, %rbx
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	movl	%ebp, 44(%rsp)
	imull	%edi, %ebp
	movl	%edi, 40(%rsp)
	leaq	48(%rsp), %rdi
	movslq	%ebp, %rbp
	leaq	0(,%rbp,4), %rax
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L32
	leaq	.LC2(%rip), %rsi
	movl	%eax, %ebp
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L32:
	movl	%r13d, 56(%rsp)
	imull	%r12d, %r13d
	leaq	64(%rsp), %rdi
	movl	%r12d, 60(%rsp)
	movslq	%r13d, %r13
	leaq	0(,%r13,4), %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L33
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L33:
	movl	%r12d, %eax
	movl	%ebx, 72(%rsp)
	imull	%ebx, %eax
	movl	%r12d, 76(%rsp)
	cltq
	salq	$2, %rax
	movq	%rax, %rbp
	movq	%rax, %rdi
	call	malloc@PLT
	leaq	80(%rsp), %rdi
	movq	%rbp, %rsi
	movq	%rax, %r13
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L34
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L34:
	movq	(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movq	%r15, %rsi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L35
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L35:
	movq	8(%rsp), %rdx
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movq	%r14, %rsi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L36
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L36:
	leal	31(%r12), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	shrl	$5, %eax
	movl	$1, %esi
	movabsq	$137438953504, %rdx
	movl	%eax, 28(%rsp)
	leal	31(%rbx), %eax
	shrl	$5, %eax
	movl	%eax, 32(%rsp)
	movq	28(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L37
	movups	40(%rsp), %xmm0
	movups	56(%rsp), %xmm1
	leaq	120(%rsp), %rdx
	leaq	104(%rsp), %rsi
	movups	72(%rsp), %xmm2
	leaq	88(%rsp), %rdi
	movups	%xmm0, 88(%rsp)
	movups	%xmm1, 104(%rsp)
	movups	%xmm2, 120(%rsp)
	call	_Z50__device_stub__Z21multiply_kernel_smart6MatrixS_S_RK6MatrixS1_RS_
.L37:
	movq	80(%rsp), %rsi
	movq	%rbp, %rdx
	movl	$2, %ecx
	movq	%r13, %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	je	.L38
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L38:
	salq	$32, %r12
	movl	%ebx, %eax
	orq	%r12, %rax
	movq	136(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$152, %rsp
	.cfi_def_cfa_offset 56
	movq	%r13, %rdx
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z8multiply6MatrixS_, .-_Z8multiply6MatrixS_
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%edi, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rax
	.cfi_def_cfa_offset 64
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	movl	$512, %esi
	movl	$512, %edi
	call	_Z6randomii
	movl	$512, %esi
	movl	$512, %edi
	movq	%rax, %r15
	movq	%rdx, %r14
	call	_Z6randomii
	movq	%r15, %rdi
	movq	%r14, %rsi
	movq	%rdx, %rbx
	movq	%rax, %rdx
	movq	%rax, %rbp
	movq	%rbx, %rcx
	call	_Z8multiply6MatrixS_
	movq	%r15, %rdi
	movq	%r14, %rsi
	movq	%rax, %r13
	movq	%rdx, %r12
	call	_Z12print_matrix6Matrix
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	call	_Z12print_matrix6Matrix
	movq	%r13, %rdi
	movq	%r12, %rsi
	call	_Z12print_matrix6Matrix
	popq	%rdx
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z21multiply_kernel_smart6MatrixS_S_"
.LC10:
	.string	"_Z22multiply_kernel_stupid6MatrixS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z21multiply_kernel_smart6MatrixS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC10(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z22multiply_kernel_stupid6MatrixS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2061:
	.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
	.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:

//question:
//does contention for accessing global memory affect read performance?
//for instance, n threads access each of the n rows in a column at the same time.
//would it be faster for thread i to access row i % n first and proceed to i + j % n?

#define TILE_WIDTH 32
#define DIVIDE_ROUND_UP(a, b)((a + b - 1) / b)
#define GET_INDEX(row, column, numcols)(row * numcols + column)


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//define matrix type
typedef struct{
  int row_count;
  int column_count;
  int* elements;
} Matrix;


__global__ void multiply_kernel_stupid(const Matrix left, const Matrix right, Matrix result);
Matrix ones(int row_count, int column_count);
Matrix random(int row_count, int column_count);
Matrix multiply(Matrix left, Matrix right);
void print_matrix(Matrix mat);

int main(){
  //make the matrices you want to multiply
  srand(time(NULL));
  Matrix A = random(512, 512);
  Matrix B = random(512, 512);
  Matrix result = multiply(A, B);
  print_matrix(A);
  print_matrix(B);
  print_matrix(result);

}

//global memory
__global__ void multiply_kernel_stupid(const Matrix left, const Matrix right, Matrix result){
  int sum = 0;
  int row_index = blockIdx.y * blockDim.y + threadIdx.y;
  int column_index = blockIdx.x * blockDim.x + threadIdx.x;
  for(int i = 0; i < left.column_count; i++){
    int left_index = row_index * left.column_count  + i;
    int right_index = column_index + i * right.column_count;
    sum += left.elements[left_index] * right.elements[right_index];
  }
  result.elements[row_index * result.row_count + column_index] = sum;

}

//better kernel, shared memory
__global__ void multiply_kernel_smart(const Matrix left, const Matrix right, Matrix result){
  // allocate shared memory
  __shared__ int left_shared[TILE_WIDTH*TILE_WIDTH];
  __shared__ int right_shared[TILE_WIDTH*TILE_WIDTH];

  //get row/col indices
  int result_row_index = threadIdx.y + blockDim.y * blockIdx.y;
  int result_col_index = threadIdx.x + blockDim.x * blockIdx.x;
  //int grid_row = blockIdx.y;
  //int grid_col = blockIdx.x;
  int block_row = threadIdx.y;
  int block_col = threadIdx.x;

  //how many blocks do we need to multiply?
  int num_block_mult = DIVIDE_ROUND_UP(left.column_count, blockDim.x);

  //loop through the tiles that we need to multiply
  for(int i = 0; i < num_block_mult; i++){
    //copy relevant blocks to shared memory, watch out for overflow
    int left_col_index = i * blockDim.x + block_col;
    int& left_row_index = result_row_index; //different name for readability
    int& right_col_index = result_col_index; //different name for readability
    int right_row_index = i * blockDim.x + block_row;
    int shared_array_index = block_col + block_row * blockDim.x;
    int left_index = GET_INDEX(left_row_index, left_col_index, left.column_count);
    int right_index = GET_INDEX(right_row_index, right_col_index, right.column_count);

    if(left_col_index < left.column_count && left_row_index < left.row_count){
      left_shared[shared_array_index] = left.elements[left_index];
    }
    else{
      left_shared[shared_array_index] = 0;
    }

    if(right_col_index < right.column_count && right_row_index < right.row_count){
      right_shared[shared_array_index] = right.elements[right_index];
    }
    else{
      right_shared[shared_array_index] = 0;
    }

    //make sure threads all finish copying to shared memory before doing multiplications
    __syncthreads();

    //do multiplications
    int dot_product = 0;
    for(int k = 0; k < blockDim.x; k++){
      dot_product += left_shared[GET_INDEX(blockIdx.y, k, blockDim.x)] * right_shared[GET_INDEX(k, blockIdx.x, blockDim.x)];
    }

    //make sure threads all finish multiplying before writing resulting block to memory
    __syncthreads();

    //write to global memory
    result.elements[GET_INDEX(result_row_index, result_col_index, result.column_count)] = dot_product;
  }

}

Matrix multiply(Matrix left, Matrix right){
  hipError_t error;
  //step 1: allocate memory on the kernel for left, right, result
  Matrix left_d, right_d;
  left_d.row_count = left.row_count;
  left_d.column_count = left.column_count;
  size_t left_size = left.row_count * left.column_count * sizeof(int);
  error = hipMalloc((void**) &left_d.elements, left_size);
  if(error != hipSuccess){
    printf("error allocating left matrix\n");
    printf("CUDA error: %s\n", hipGetErrorString(error));
  }


  right_d.row_count = right.row_count;
  right_d.column_count = right.column_count;
  size_t right_size = right.row_count * right.column_count * sizeof(int);
  error = hipMalloc((void**) &right_d.elements, right_size);
  if(error != hipSuccess){ printf("error allocating right matrix\n"); }

  //step 2: allocate memory on the host and device for result
  Matrix result, result_d;
  result.row_count = result_d.row_count = left.row_count;
  result.column_count = result_d.column_count = right.column_count;
  size_t result_size = result.row_count * result.column_count * sizeof(int);
  result.elements = (int*) malloc(result_size);
  error = hipMalloc((void**) &result_d.elements, result_size);
  if(error != hipSuccess){ printf("error allocating matrix\n"); }

  //step 3: copy left and right to device
  error = hipMemcpy(left_d.elements, left.elements, left_size, hipMemcpyHostToDevice);
  if(error != hipSuccess){ printf("error copying left matrix\n"); }
  error = hipMemcpy(right_d.elements, right.elements, right_size, hipMemcpyHostToDevice);
  if(error != hipSuccess){ printf("error copying right matrix\n"); }

  //step 4: launch kernel



  dim3 block_dims(TILE_WIDTH, TILE_WIDTH);
  dim3 grid_dims(DIVIDE_ROUND_UP(result.column_count, block_dims.x), DIVIDE_ROUND_UP(result.row_count, block_dims.y));
  multiply_kernel_smart <<<grid_dims, block_dims>>> (left_d, right_d, result_d);

  //step 5: copy results back to host
  error = hipMemcpy(result.elements, result_d.elements, result_size, hipMemcpyDeviceToHost);
  if(error != hipSuccess){
  	printf("error copying result matrix\n");
  	printf("CUDA error: %s\n", hipGetErrorString(error));
  }
  return result;
}

Matrix ones (int row_count, int column_count){
  Matrix result;
  result.row_count = row_count;
  result.column_count = column_count;
  result.elements = (int*) malloc(row_count * column_count * sizeof(int));
  for(int i = 0; i < row_count * column_count; i++){
    result.elements[i] = 1;
  }
  return result;
}

Matrix random (int row_count, int column_count){
  Matrix result;
  result.row_count = row_count;
  result.column_count = column_count;
  result.elements = (int*) malloc(row_count * column_count * sizeof(int));
  for(int i = 0; i < row_count * column_count; i++){
    result.elements[i] = rand() % 100;
  }
  return result;
}

void print_matrix(Matrix mat){
  int num_elements = mat.row_count * mat.column_count;
  for(int i = 0; i < num_elements; i++){
    printf(" %d", mat.elements[i]);
    if(!((i + 1) % mat.column_count)){ printf("\n"); }
  }
}

	.text
	.file	"cudaMatrixMultiply.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$512, %edi                      # imm = 0x200
	movl	$512, %esi                      # imm = 0x200
	callq	_Z6randomii
	movq	%rax, %rbx
	movq	%rdx, %r14
	movl	$512, %edi                      # imm = 0x200
	movl	$512, %esi                      # imm = 0x200
	callq	_Z6randomii
	movq	%rax, %r15
	movq	%rdx, %r12
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movq	%rax, %rdx
	movq	%r12, %rcx
	callq	_Z8multiply6MatrixS_
	movq	%rax, %r13
	movq	%rdx, %rbp
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_Z12print_matrix6Matrix
	movq	%r15, %rdi
	movq	%r12, %rsi
	callq	_Z12print_matrix6Matrix
	movq	%r13, %rdi
	movq	%rbp, %rsi
	callq	_Z12print_matrix6Matrix
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z6randomii                     # -- Begin function _Z6randomii
	.type	_Z6randomii,@function
_Z6randomii:                            # @_Z6randomii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebx
	movl	%edi, %ebp
	movl	%esi, %r15d
	imull	%edi, %r15d
	movslq	%r15d, %r12
	leaq	(,%r12,4), %rdi
	callq	malloc
	movq	%rax, %r14
	testl	%r12d, %r12d
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%r15d, %r15d
	xorl	%r12d, %r12d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$37, %rcx
	addl	%edx, %ecx
	imull	$100, %ecx, %ecx
	subl	%ecx, %eax
	movl	%eax, (%r14,%r12,4)
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	shlq	$32, %rbx
	movl	%ebp, %eax
	orq	%rbx, %rax
	movq	%r14, %rdx
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z6randomii, .Lfunc_end1-_Z6randomii
	.cfi_endproc
                                        # -- End function
	.globl	_Z8multiply6MatrixS_            # -- Begin function _Z8multiply6MatrixS_
	.type	_Z8multiply6MatrixS_,@function
_Z8multiply6MatrixS_:                   # @_Z8multiply6MatrixS_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rcx, 16(%rsp)                  # 8-byte Spill
	movq	%rdx, %r12
	movq	%rsi, 8(%rsp)                   # 8-byte Spill
	movq	%rdi, %r13
	movq	%rdi, %r14
	shrq	$32, %r14
	movq	%rdx, %rbx
	shrq	$32, %rbx
	leaq	80(%rsp), %rdi
	movq	%r13, -8(%rdi)
	movq	%r13, %r15
	shlq	$32, %r15
	imulq	%r15, %r14
	sarq	$30, %r14
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB2_2
# %bb.1:
	movl	%eax, %ebp
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	%ebp, %edi
	callq	hipGetErrorString
	movl	$.L.str.1, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB2_2:
	leaq	64(%rsp), %rdi
	movl	%r12d, -8(%rdi)
	movl	%ebx, -4(%rdi)
	movq	%r12, 32(%rsp)                  # 8-byte Spill
	movl	%r12d, %eax
	imull	%ebx, %eax
	movslq	%eax, %rbp
	shlq	$2, %rbp
	movq	%rbp, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB2_4
# %bb.3:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
.LBB2_4:
	leaq	48(%rsp), %r12
	movl	%r13d, -8(%r12)
	movl	%ebx, -4(%r12)
	imulq	%rbx, %r15
	sarq	$30, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB2_6
# %bb.5:
	movl	$.Lstr.2, %edi
	callq	puts@PLT
.LBB2_6:
	movq	80(%rsp), %rdi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB2_8
# %bb.7:
	movl	$.Lstr.3, %edi
	callq	puts@PLT
.LBB2_8:
	movq	64(%rsp), %rdi
	movq	16(%rsp), %rsi                  # 8-byte Reload
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	movq	32(%rsp), %r14                  # 8-byte Reload
	je	.LBB2_10
# %bb.9:
	movl	$.Lstr.4, %edi
	callq	puts@PLT
.LBB2_10:
	addq	$31, %rbx
	shrq	$5, %rbx
	movq	%r13, %rax
	shlq	$27, %rax
	movl	$4160749568, %ecx               # imm = 0xF8000000
	addq	%rax, %rcx
	orq	%rbx, %rcx
	movabsq	$576460748142673919, %rdi       # imm = 0x7FFFFFF07FFFFFF
	andq	%rcx, %rdi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_12
# %bb.11:
	movq	72(%rsp), %rdi
	movq	80(%rsp), %rsi
	movq	56(%rsp), %rdx
	movq	64(%rsp), %rcx
	movq	40(%rsp), %r8
	movq	48(%rsp), %r9
	callq	_Z36__device_stub__multiply_kernel_smart6MatrixS_S_
.LBB2_12:
	movq	48(%rsp), %rsi
	movq	24(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	je	.LBB2_14
# %bb.13:
	movl	%eax, %ebp
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	movl	%ebp, %edi
	callq	hipGetErrorString
	movl	$.L.str.1, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB2_14:
	movabsq	$-4294967296, %rax              # imm = 0xFFFFFFFF00000000
	andq	%rax, %r14
	movl	%r13d, %eax
	orq	%r14, %rax
	movq	%rbx, %rdx
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z8multiply6MatrixS_, .Lfunc_end2-_Z8multiply6MatrixS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_matrix6Matrix         # -- Begin function _Z12print_matrix6Matrix
	.type	_Z12print_matrix6Matrix,@function
_Z12print_matrix6Matrix:                # @_Z12print_matrix6Matrix
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %r15
	shrq	$32, %r15
	movl	%r15d, %eax
	imull	%edi, %eax
	testl	%eax, %eax
	jle	.LBB3_5
# %bb.1:                                # %.lr.ph.preheader
	movq	%rsi, %rbx
	movl	%eax, %r12d
	xorl	%r14d, %r14d
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r14,4), %esi
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r14
	movl	%r14d, %eax
	cltd
	idivl	%r15d
	testl	%edx, %edx
	jne	.LBB3_4
# %bb.3:                                #   in Loop: Header=BB3_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
.LBB3_4:                                #   in Loop: Header=BB3_2 Depth=1
	cmpq	%r14, %r12
	jne	.LBB3_2
.LBB3_5:                                # %._crit_edge
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z12print_matrix6Matrix, .Lfunc_end3-_Z12print_matrix6Matrix
	.cfi_endproc
                                        # -- End function
	.globl	_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_ # -- Begin function _Z37__device_stub__multiply_kernel_stupid6MatrixS_S_
	.type	_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_,@function
_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_: # @_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	80(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsi, 8(%rax)
	leaq	64(%rsp), %rsi
	movq	%rdx, (%rsi)
	movq	%rcx, 8(%rsi)
	leaq	48(%rsp), %rcx
	movq	%r8, (%rcx)
	movq	%r9, 8(%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rsi, 8(%rbx)
	movq	%rcx, 16(%rbx)
	leaq	32(%rsp), %r14
	leaq	16(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z22multiply_kernel_stupid6MatrixS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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_end4:
	.size	_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_, .Lfunc_end4-_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z36__device_stub__multiply_kernel_smart6MatrixS_S_ # -- Begin function _Z36__device_stub__multiply_kernel_smart6MatrixS_S_
	.type	_Z36__device_stub__multiply_kernel_smart6MatrixS_S_,@function
_Z36__device_stub__multiply_kernel_smart6MatrixS_S_: # @_Z36__device_stub__multiply_kernel_smart6MatrixS_S_
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	80(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsi, 8(%rax)
	leaq	64(%rsp), %rsi
	movq	%rdx, (%rsi)
	movq	%rcx, 8(%rsi)
	leaq	48(%rsp), %rcx
	movq	%r8, (%rcx)
	movq	%r9, 8(%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rsi, 8(%rbx)
	movq	%rcx, 16(%rbx)
	leaq	32(%rsp), %r14
	leaq	16(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z21multiply_kernel_smart6MatrixS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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_end5:
	.size	_Z36__device_stub__multiply_kernel_smart6MatrixS_S_, .Lfunc_end5-_Z36__device_stub__multiply_kernel_smart6MatrixS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z4onesii                       # -- Begin function _Z4onesii
	.type	_Z4onesii,@function
_Z4onesii:                              # @_Z4onesii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebx
	movl	%edi, %ebp
	movl	%esi, %r14d
	imull	%edi, %r14d
	movslq	%r14d, %r15
	leaq	(,%r15,4), %rdi
	callq	malloc
	movq	%rax, %rdx
	testl	%r15d, %r15d
	jle	.LBB6_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%r14d, %eax
	xorl	%ecx, %ecx
.LBB6_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$1, (%rdx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB6_2
.LBB6_3:                                # %._crit_edge
	shlq	$32, %rbx
	movl	%ebp, %eax
	orq	%rbx, %rax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z4onesii, .Lfunc_end6-_Z4onesii
	.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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z22multiply_kernel_stupid6MatrixS_S_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z21multiply_kernel_smart6MatrixS_S_, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__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	.LBB8_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
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z22multiply_kernel_stupid6MatrixS_S_,@object # @_Z22multiply_kernel_stupid6MatrixS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z22multiply_kernel_stupid6MatrixS_S_
	.p2align	3, 0x0
_Z22multiply_kernel_stupid6MatrixS_S_:
	.quad	_Z37__device_stub__multiply_kernel_stupid6MatrixS_S_
	.size	_Z22multiply_kernel_stupid6MatrixS_S_, 8

	.type	_Z21multiply_kernel_smart6MatrixS_S_,@object # @_Z21multiply_kernel_smart6MatrixS_S_
	.globl	_Z21multiply_kernel_smart6MatrixS_S_
	.p2align	3, 0x0
_Z21multiply_kernel_smart6MatrixS_S_:
	.quad	_Z36__device_stub__multiply_kernel_smart6MatrixS_S_
	.size	_Z21multiply_kernel_smart6MatrixS_S_, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"CUDA error: %s\n"
	.size	.L.str.1, 16

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	" %d"
	.size	.L.str.7, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z22multiply_kernel_stupid6MatrixS_S_"
	.size	.L__unnamed_1, 38

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z21multiply_kernel_smart6MatrixS_S_"
	.size	.L__unnamed_2, 37

	.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	"error allocating left matrix"
	.size	.Lstr, 29

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"error allocating right matrix"
	.size	.Lstr.1, 30

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"error allocating matrix"
	.size	.Lstr.2, 24

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"error copying left matrix"
	.size	.Lstr.3, 26

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"error copying right matrix"
	.size	.Lstr.4, 27

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"error copying result matrix"
	.size	.Lstr.5, 28

	.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 _Z37__device_stub__multiply_kernel_stupid6MatrixS_S_
	.addrsig_sym _Z36__device_stub__multiply_kernel_smart6MatrixS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z22multiply_kernel_stupid6MatrixS_S_
	.addrsig_sym _Z21multiply_kernel_smart6MatrixS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z22multiply_kernel_stupid6MatrixS_S_ ; -- Begin function _Z22multiply_kernel_stupid6MatrixS_S_
	.globl	_Z22multiply_kernel_stupid6MatrixS_S_
	.p2align	8
	.type	_Z22multiply_kernel_stupid6MatrixS_S_,@function
_Z22multiply_kernel_stupid6MatrixS_S_:  ; @_Z22multiply_kernel_stupid6MatrixS_S_
; %bb.0:
	s_clause 0x2
	s_load_b32 s4, s[0:1], 0x3c
	s_load_b32 s6, s[0:1], 0x4
	s_load_b64 s[2:3], s[0:1], 0x28
	v_bfe_u32 v2, v0, 10, 10
	v_and_b32_e32 v3, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s5, s4, 16
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u64_u32 v[0:1], null, s15, s5, v[2:3]
	v_mad_u64_u32 v[1:2], null, s14, s4, v[3:4]
	s_cmp_lt_i32 s6, 1
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph
	s_load_b64 s[8:9], s[0:1], 0x8
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v2, v0, s6
	s_clause 0x1
	s_load_b32 s7, s[0:1], 0x14
	s_load_b64 s[4:5], s[0:1], 0x18
	v_mov_b32_e32 v5, v1
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[3:4], 2, v[2:3]
	v_mov_b32_e32 v2, 0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v3, vcc_lo, s8, v3
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v4, vcc_lo, s9, v4, vcc_lo
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v6, 31, v5
	s_add_i32 s6, s6, -1
	s_cmp_eq_u32 s6, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[6:7], 2, v[5:6]
	v_add_co_u32 v6, vcc_lo, s4, v6
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v7, vcc_lo
	global_load_b32 v8, v[3:4], off
	global_load_b32 v9, v[6:7], off
	s_waitcnt vmcnt(0)
	v_mad_u64_u32 v[6:7], null, v9, v8, v[2:3]
	v_add_co_u32 v3, vcc_lo, v3, 4
	v_add_co_ci_u32_e32 v4, vcc_lo, 0, v4, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_dual_mov_b32 v2, v6 :: v_dual_add_nc_u32 v5, s7, v5
	s_cbranch_scc0 .LBB0_2
	s_branch .LBB0_4
.LBB0_3:
	v_mov_b32_e32 v2, 0
.LBB0_4:                                ; %Flow31
	s_load_b32 s0, s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[3:4], null, v0, s0, v[1:2]
	v_ashrrev_i32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[3:4]
	v_add_co_u32 v0, vcc_lo, s2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z22multiply_kernel_stupid6MatrixS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.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 10
		.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	_Z22multiply_kernel_stupid6MatrixS_S_, .Lfunc_end0-_Z22multiply_kernel_stupid6MatrixS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 356
; NumSgprs: 18
; NumVgprs: 10
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 10
; 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
	.protected	_Z21multiply_kernel_smart6MatrixS_S_ ; -- Begin function _Z21multiply_kernel_smart6MatrixS_S_
	.globl	_Z21multiply_kernel_smart6MatrixS_S_
	.p2align	8
	.type	_Z21multiply_kernel_smart6MatrixS_S_,@function
_Z21multiply_kernel_smart6MatrixS_S_:   ; @_Z21multiply_kernel_smart6MatrixS_S_
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x3c
	s_load_b32 s9, s[0:1], 0x4
	s_add_u32 s2, s0, 48
	s_addc_u32 s3, s1, 0
	s_mov_b32 s11, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_cvt_f32_u32_e32 v1, s8
	s_sub_i32 s5, 0, s8
	s_add_i32 s6, s9, s8
	s_add_i32 s6, s6, -1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_readfirstlane_b32 s4, v1
	s_mul_i32 s5, s5, s4
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s5, s4, s5
	s_add_i32 s4, s4, s5
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s4, s6, s4
	s_mul_i32 s5, s4, s8
	s_delay_alu instid0(SALU_CYCLE_1)
	s_sub_i32 s5, s6, s5
	s_add_i32 s6, s4, 1
	s_sub_i32 s7, s5, s8
	s_cmp_ge_u32 s5, s8
	s_cselect_b32 s4, s6, s4
	s_cselect_b32 s5, s7, s5
	s_add_i32 s6, s4, 1
	s_cmp_ge_u32 s5, s8
	s_cselect_b32 s10, s6, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_i32 s10, 1
	s_cbranch_scc1 .LBB1_9
; %bb.1:                                ; %.lr.ph
	s_load_b32 s2, s[2:3], 0xc
	s_load_b32 s3, s[0:1], 0x24
	v_and_b32_e32 v1, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	s_clause 0x2
	s_load_b128 s[4:7], s[0:1], 0x8
	s_load_b32 s13, s[0:1], 0x0
	s_load_b64 s[16:17], s[0:1], 0x28
	s_max_u32 s12, s8, 1
	v_mad_u64_u32 v[2:3], null, s14, s8, v[1:2]
	s_lshl_b32 s14, s14, 2
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s2, s2, 16
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	v_cmp_gt_i32_e32 vcc_lo, s7, v2
	v_mad_u64_u32 v[3:4], null, s15, s2, v[0:1]
	v_mul_u32_u24_e32 v4, s8, v0
	s_mul_i32 s15, s15, s8
	s_lshl_b32 s15, s15, 2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_lshl_u32 v5, v4, v1, 2
	v_mad_u64_u32 v[6:7], null, v3, s3, v[2:3]
	s_load_b64 s[2:3], s[0:1], 0x18
	v_cmp_gt_i32_e64 s0, s13, v3
	s_add_i32 s13, s14, 0x1000
	s_lshl_b32 s14, s8, 2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v7, 31, v6
	v_lshlrev_b64 v[8:9], 2, v[6:7]
	v_mul_lo_u32 v7, v3, s9
	v_add_nc_u32_e32 v6, 0x1000, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v3, s1, s16, v8
	v_add_co_ci_u32_e64 v4, s1, s17, v9, s1
.LBB1_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB1_7 Depth 2
	s_mul_i32 s16, s11, s8
	v_mov_b32_e32 v8, 0
	v_dual_mov_b32 v9, 0 :: v_dual_add_nc_u32 v10, s16, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_gt_i32_e64 s1, s9, v10
	s_and_b32 s1, s1, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s17, s1
	s_cbranch_execz .LBB1_4
; %bb.3:                                ;   in Loop: Header=BB1_2 Depth=1
	v_add_nc_u32_e32 v9, v10, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v10, 31, v9
	v_lshlrev_b64 v[9:10], 2, v[9:10]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v9, s1, s4, v9
	v_add_co_ci_u32_e64 v10, s1, s5, v10, s1
	global_load_b32 v9, v[9:10], off
.LBB1_4:                                ;   in Loop: Header=BB1_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s17
	v_add_nc_u32_e32 v10, s16, v0
	s_waitcnt vmcnt(0)
	ds_store_b32 v5, v9
	v_cmp_gt_i32_e64 s1, s6, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s1, vcc_lo, s1
	s_and_saveexec_b32 s16, s1
	s_cbranch_execz .LBB1_6
; %bb.5:                                ;   in Loop: Header=BB1_2 Depth=1
	v_mad_u64_u32 v[8:9], null, v10, s7, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v9, 31, v8
	v_lshlrev_b64 v[8:9], 2, v[8:9]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v8, s1, s2, v8
	v_add_co_ci_u32_e64 v9, s1, s3, v9, s1
	global_load_b32 v8, v[8:9], off
.LBB1_6:                                ;   in Loop: Header=BB1_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s16
	s_waitcnt vmcnt(0)
	ds_store_b32 v6, v8
	v_mov_b32_e32 v8, 0
	s_mov_b32 s1, s15
	s_mov_b32 s16, s13
	s_mov_b32 s17, s12
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB1_7:                                ;   Parent Loop BB1_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_dual_mov_b32 v9, s1 :: v_dual_mov_b32 v10, s16
	s_add_i32 s17, s17, -1
	s_add_i32 s16, s16, s14
	s_add_i32 s1, s1, 4
	ds_load_b32 v9, v9
	ds_load_b32 v10, v10
	s_cmp_eq_u32 s17, 0
	s_waitcnt lgkmcnt(0)
	v_mul_lo_u32 v9, v10, v9
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v8, v9, v8
	s_cbranch_scc0 .LBB1_7
; %bb.8:                                ;   in Loop: Header=BB1_2 Depth=1
	s_add_i32 s11, s11, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_i32 s11, s10
	s_barrier
	buffer_gl0_inv
	global_store_b32 v[3:4], v8, off
	s_cbranch_scc0 .LBB1_2
.LBB1_9:                                ; %._crit_edge
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z21multiply_kernel_smart6MatrixS_S_
		.amdhsa_group_segment_fixed_size 8192
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.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 11
		.amdhsa_next_free_sgpr 18
		.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_end1:
	.size	_Z21multiply_kernel_smart6MatrixS_S_, .Lfunc_end1-_Z21multiply_kernel_smart6MatrixS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 744
; NumSgprs: 20
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 8192 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 20
; NumVGPRsForWavesPerEU: 11
; 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:
      - .offset:         0
        .size:           16
        .value_kind:     by_value
      - .offset:         16
        .size:           16
        .value_kind:     by_value
      - .offset:         32
        .size:           16
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z22multiply_kernel_stupid6MatrixS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z22multiply_kernel_stupid6MatrixS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     10
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .offset:         0
        .size:           16
        .value_kind:     by_value
      - .offset:         16
        .size:           16
        .value_kind:     by_value
      - .offset:         32
        .size:           16
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 8192
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z21multiply_kernel_smart6MatrixS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     20
    .sgpr_spill_count: 0
    .symbol:         _Z21multiply_kernel_smart6MatrixS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
