Skip to content

Commit

Permalink
Merge pull request #188 from RadeonOpenCompute/fix_extractkernel
Browse files Browse the repository at this point in the history
Fix extractkernel
  • Loading branch information
scchan authored Dec 12, 2016
2 parents 10979fc + 13ba8e1 commit 71c6149
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions lib/extractkernel.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ sub usage {
print("-h \t\t\t\tshow this help message\n");
print("-i <input> \t\t\t\tinput file\n");
print("-t <target> \t\t\t\ttarget architecture (optional)\n");
print(" \t\t\t\t 7:0:0 for CI (Kaveri)\n");
print(" \t\t\t\t 8:0:1 for subset of VI (Carizzo)\n");
print(" \t\t\t\t 8:0:3 for subset of VI (Fiji) (default)\n");
print(" \t\t\t\t gfx700 for CI (Kaveri)\n");
print(" \t\t\t\t gfx801 for subset of VI (Carizzo)\n");
print(" \t\t\t\t gfx803 for subset of VI (Fiji) (default)\n");
exit;
}

Expand All @@ -29,51 +29,37 @@ $input_file = $options{i};
(-f $input_file) || die("can't find $input_file");

# Set default target architecture to Fiji
my $target = "8:0:3";
my $target = "gfx803";
if (defined $options{t}) {
$target = $options{t};
}

my $brig_start_tag = "_binary_kernel_brig_start";
my $brig_size_tag = "_binary_kernel_brig_size";
my $brig_start = hex(`objdump -t $input_file | grep $brig_start_tag | awk '{print \$1}'`);
my $brig_size = hex(`objdump -t $input_file | grep $brig_size_tag | awk '{print \$1}'`);
my $command;

if ($brig_start == 0 || $brig_size == 0) {
my $binary_start_tag = "_binary_kernel_brig_start";
my $binary_size_tag = "_binary_kernel_brig_size";

$command = "objdump -t $input_file | grep $binary_start_tag | awk '{print \$1}'";
my $binary_start = hex(`$command`);
$command = "objdump -t $input_file | grep $binary_size_tag | awk '{print \$1}'";
my $binary_size = hex(`$command`);

if ($binary_start == 0 || $binary_size == 0) {
print("can't find any hsail kernel in $input_file\n");
}
else {

# extract the hsail brig file from the binary
# extract the GPU binary file from the host binary
my $data_lma = hex(`objdump -h $input_file | grep ".data.rel.ro" | awk '{print \$5}'`);
my $data_file_offset = hex(`objdump -h $input_file | grep ".data.rel.ro" | awk '{print \$6}'`);
my $brig_file_offset = $data_file_offset + ($brig_start - $data_lma);
my $brig_file_name = "$input_file.brig";
system("dd if=$input_file of=$brig_file_name skip=$brig_file_offset count=$brig_size bs=1 status=none");
my $binary_file_offset = $data_file_offset + ($binary_start - $data_lma);
my $binary_file_name = "$input_file.hsaco";
system("dd if=$input_file of=$binary_file_name skip=$binary_file_offset count=$binary_size bs=1 status=none");

my $hsa_path = "@HSA_ROOT@";
my $rocm_path = "@ROCM_ROOT@";
my $llvm_objdump = "$rocm_path/hcc-lc/llvm/bin/llvm-objdump";
(-f $llvm_objdump) || die("can't find llvm-objdump to diassemble the GPU binary");

# set the default to rocm path
my $hsafin = "$rocm_path/bin/amdhsafin";

if (defined $ENV{'HSA_HOME'}) {
$hsafin = "$ENV{'HSA_HOME'}/bin/amdhsafin";
}
elsif (!(-f $hsafin)) {
# can't find amdhsafin in rocm, let's try with hsa root
$hsafin = "$hsa_path/bin/amdhsafin";
}

if (-f $hsafin) {

# use the offline finalizer to dump out the hsail and isa
system("$hsafin -target=$target -brig $brig_file_name -output=$input_file.gpu.o -O2 -dump-isa -dump-hsail");
move("./amdhsa001.hsail","./$input_file.hsail");
move("./amdhsa001.isa","./$input_file.isa");
}
else {
print("can't find HSAIL offline finalizer\n");
}

$command = "$llvm_objdump -disassemble -mcpu=$target $binary_file_name > $input_file.$target.isa";
system($command);
}

0 comments on commit 71c6149

Please sign in to comment.