Issue 152281
Summary RISC-V: How is a constant’s data type determined when it takes part in an operation.
Labels new issue
Assignees
Reporter dongyasun
    Hello,
While working with the `zfbfmin` extension on RISC-V, I observed an interesting behavior: when performing operations between bf16 values and constants, the assembler does not allow the constant to be loaded using the `flh` instruction.   
clang version 19.1.7   

~~~
#include <stdio.h>

__bf16 a = 1.11;
__bf16 b = 11.8;
const __bf16 c = 10.3;

void test_add(){
    __bf16 sum = a + b;
}


int main(void)
{
  return 0;
}
~~~
In the code above, compiling with `clang -march=rv64gc_zfbfmin --target=riscv64-unknown-elf -S bf16_fadd.c` yields the following assembly:   

<img width="653" height="500" alt="Image" src="" />      
       
 
When I replace the line `__bf16 sum = a + b` in the code above with either `__bf16 sum = a + 5` or `__bf16 sum = a + c`, the generated assembly becomes:    

<img width="611" height="472" alt="Image" src="" />  
   

When all the bf16 types in the above code are replaced with f16, as shown below:      

~~~  
#include <stdio.h>

typedef _Float16 f16;

f16 a = 1.11;
f16 b = 11.8;
const f16 c = 10.3;
void test_add(){
    f16 sum = a + b;
}

int main(void)
{
  return 0;
}
~~~  

Now, regardless of whether the _expression_ is `a + b`, `a + 5`, or `a + c`, the same assembly is generated when using the command: `clang -march=rv64gc_zfh --target=riscv64-unknown-elf -S f16_fadd.c`.            

<img width="614" height="409" alt="Image" src="" />   
 
    

So I’m curious: once bf16 addition is fully implemented, will its constants, like those of type f16, be treated as bf16 values directly, without any extra conversion?        


Furthermore, when the _expression_ `a + 5` in the code above is changed to `a + 5.5`, the literal 5.5 is treated directly as a double, and the resulting assembly is shown below:      

<img width="618" height="457" alt="Image" src="" />    
  

So how is a constant’s data type determined when it takes part in an operation?
Which piece of code makes that choice—
is it governed by the code that deals with the type itself, or by the code that handles the operation?

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to