Skip to content

Implement the mul HLSL Function #99138

@farzonl

Description

@farzonl
  • Implement mul clang builtin,
  • Link mul clang builtin with hlsl_intrinsics.h
  • Add sema checks for mul to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for mul to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/mul.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/mul-errors.hlsl
  • Create the int_spv_mul intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the mul lowering and map it to int_spv_mul in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/mul.ll

DirectX

There were no DXIL opcodes found for mul.

SPIR-V

OpFMul:

Description:

Floating-point multiplication of Operand 1 and Operand 2.

Result Type must be a scalar or vector of floating-point
type
.

The types of Operand 1 and Operand 2 both must be the same as
Result Type.

Results are computed per component.

Word Count Opcode Results Operands

5

133

<id>
Result Type

Result <id>

<id>
Operand 1

<id>
Operand 2

Test Case(s)

Example 1

//dxc mul_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float fn(float p1, float p2) {
    return mul(p1, p2);
}

Example 2

//dxc mul_1_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint fn(uint p1, uint p2) {
    return mul(p1, p2);
}

Example 3

//dxc mul_2_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int fn(int p1, int p2) {
    return mul(p1, p2);
}

Example 4

//dxc mul_3_test.hlsl -T lib_6_8  -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

export float4 fn(float4 p1, float p2) {
    return mul(p1, p2);
}

Example 5

//dxc mul_4_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float p1, float4 p2) {
    return mul(p1, p2);
}

Example 6

//dxc mul_5_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(uint p1, uint4 p2) {
    return mul(p1, p2);
}

Example 7

//dxc mul_6_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4 fn(int p1, int4 p2) {
    return mul(p1, p2);
}

Example 8

//dxc mul_7_test.hlsl -T lib_6_8  -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

export float4 fn(float4x4 p1, float4 p2) {
    return mul(p1, p2);
}

Example 9

//dxc mul_8_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4x4 fn(float p1, float4x4 p2) {
    return mul(p1, p2);
}

Example 10

//dxc mul_9_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4x4 fn(uint p1, uint4x4 p2) {
    return mul(p1, p2);
}

Example 11

//dxc mul_10_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4x4 fn(int p1, int4x4 p2) {
    return mul(p1, p2);
}

Example 12

//dxc mul_12_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float4 p1, float p2) {
    return mul(p1, p2);
}

Example 13

//dxc mul_13_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(uint4 p1, uint p2) {
    return mul(p1, p2);
}

Example 14

//dxc mul_14_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4 fn(int4 p1, int p2) {
    return mul(p1, p2);
}

Example 15

//dxc mul_16_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float fn(float4 p1, float4 p2) {
    return mul(p1, p2);
}

Example 16

//dxc mul_17_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint fn(uint4 p1, uint4 p2) {
    return mul(p1, p2);
}

Example 17

//dxc mul_18_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int fn(int4 p1, int4 p2) {
    return mul(p1, p2);
}

Example 18

//dxc mul_20_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float4 p1, float4x4 p2) {
    return mul(p1, p2);
}

Example 19

//dxc mul_21_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(uint4 p1, uint4x4 p2) {
    return mul(p1, p2);
}

Example 20

//dxc mul_22_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4 fn(int4 p1, int4x4 p2) {
    return mul(p1, p2);
}

Example 21

//dxc mul_24_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4x4 fn(float4x4 p1, float p2) {
    return mul(p1, p2);
}

Example 22

//dxc mul_25_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4x4 fn(uint4x4 p1, uint p2) {
    return mul(p1, p2);
}

Example 23

//dxc mul_26_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4x4 fn(int4x4 p1, int p2) {
    return mul(p1, p2);
}

Example 24

//dxc mul_28_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float4x4 p1, float4 p2) {
    return mul(p1, p2);
}

Example 25

//dxc mul_29_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(uint4x4 p1, uint4 p2) {
    return mul(p1, p2);
}

Example 26

//dxc mul_30_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4 fn(int4x4 p1, int4 p2) {
    return mul(p1, p2);
}

Example 27

//dxc mul_32_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4x4 fn(float4x4 p1, float4x4 p2) {
    return mul(p1, p2);
}

Example 28

//dxc mul_33_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4x4 fn(uint4x4 p1, uint4x4 p2) {
    return mul(p1, p2);
}

Example 29

//dxc mul_34_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4x4 fn(int4x4 p1, int4x4 p2) {
    return mul(p1, p2);
}

HLSL:

Multiplies x and y using matrix math. The inner dimension x-columns and y-rows must be equal.

ret mul(x, y)

Parameters

Item Description
x
[in] The x input value. If x is a vector, it treated as a row vector.
y
[in] The y input value. If y is a vector, it treated as a column vector.

Return Value

The result of x times y. The result has the dimension x-rows x y-columns.

Type Description

There are 9 overloaded versions of this function; the overloaded versions handle the different cases for the types and sizes of the input arguments.

Version Name Purpose Template Type Component Type Size
1
x in scalar float, int 1
y in scalar same as input x 1
ret out scalar same as input x 1
2
x in scalar float, int 1
y in vector float, int any
ret out vector float, int same dimension(s) as input y
3
x in scalar float, int 1
y in matrix float, int any
ret out matrix same as input y same dimension(s) as input y
4
x in vector float, int any
y in scalar float, int 1
ret out vector float, int same dimension(s) as input x
5
x in vector float, int any
y in vector float, int same dimension(s) as input x
ret out scalar float, int 1
6
x in vector float, int any
y in matrix float, int rows = same dimension(s) as input x, columns = any
ret out vector float, int same dimension(s) as input y columns
7
x in matrix float, int any
y in scalar float, int 1
ret out matrix float, int same dimension(s) as input x
8
x in matrix float, int any
y in vector float, int number of columns in input x
ret out vector float, int number of rows in input x
9
x in matrix float, int any
y in matrix float, int rows = number of columns in input x
ret out matrix float, int rows = number of rows in input x, columns = number of columns in input y

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 1 (DirectX HLSL) and higher shader models yes

See also

Intrinsic Functions (DirectX HLSL)

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportbackend:SPIR-Vbot:HLSLmetaissueIssue to collect references to a group of similar or related issues.

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions