Skip to content

Commit 990cbc4

Browse files
committed
[CIR][X86] Implement lowering for sqrt builtins
Implements CIR IR generation for X86 sqrt builtin functions, addressing issue #167765. This change adds support for lowering the following X86 sqrt builtins to CIR operations: - __builtin_ia32_sqrtps (128-bit float vector sqrt) - __builtin_ia32_sqrtps256 (256-bit float vector sqrt) - __builtin_ia32_sqrtps512 (512-bit float vector sqrt) - __builtin_ia32_sqrtpd (128-bit double vector sqrt) - __builtin_ia32_sqrtpd256 (256-bit double vector sqrt) - __builtin_ia32_sqrtpd512 (512-bit double vector sqrt) - __builtin_ia32_sqrtph, __builtin_ia32_sqrtph256, __builtin_ia32_sqrtph512 (half precision) - __builtin_ia32_vsqrtbf16, __builtin_ia32_vsqrtbf16256, __builtin_ia32_vsqrtbf16512 (bfloat16) - __builtin_ia32_sqrtsh_round_mask, __builtin_ia32_sqrtsd_round_mask, __builtin_ia32_sqrtss_round_mask (masked rounding) Changes: - Added CIROps.h wrapper header for CIR operation declarations - Updated CIROps.td with SqrtOp definition (Pure trait, type-safe constraints) - Updated CIRTypeConstraints.td with float type constraints - Implemented builtin handling in CIRGenBuiltinX86.cpp - Added SqrtOp verification in CIRDialect.cpp - Implemented cir.sqrt lowering to llvm.sqrt.* intrinsics in LowerToLLVM.cpp/h - Added comprehensive test coverage: * cir-sqrtps-builtins.c: X86 builtin tests (128/256/512-bit variants) * sqrt-lowering.mlir: CIR to LLVM IR lowering tests (scalar and vector) The implementation follows existing CIR patterns and generates proper cir.sqrt operations for all sqrt builtin variants with appropriate type checking and lowering to LLVM IR. Related: #167765
1 parent 83f8a19 commit 990cbc4

File tree

5 files changed

+73
-42
lines changed

5 files changed

+73
-42
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- CIROps.h - CIR dialect operations ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the operations in the CIR dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CLANG_CIR_DIALECT_IR_CIROPS_H
14+
#define CLANG_CIR_DIALECT_IR_CIROPS_H
15+
16+
#include "mlir/IR/Builders.h"
17+
#include "mlir/IR/BuiltinOps.h"
18+
#include "mlir/IR/BuiltinTypes.h"
19+
#include "mlir/IR/OpDefinition.h"
20+
#include "mlir/Interfaces/InferTypeOpInterface.h"
21+
22+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
23+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
24+
25+
#include "clang/CIR/Dialect/IR/CIROps.h.inc"
26+
27+
#endif // CLANG_CIR_DIALECT_IR_CIROPS_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Test for x86 sqrt builtins (sqrtps, sqrtpd, sqrtss, sqrtsd, etc.)
2+
// RUN: %clang_cc1 -fcir -triple x86_64-unknown-linux-gnu -O0 %s -emit-cir -o - | FileCheck %s
3+
4+
#include <immintrin.h>
5+
6+
// Test __builtin_ia32_sqrtps - single precision vector sqrt (128-bit)
7+
__m128 test_sqrtps(__m128 x) {
8+
return __builtin_ia32_sqrtps(x);
9+
}
10+
// CHECK-LABEL: cir.func @test_sqrtps
11+
// CHECK: cir.sqrt
12+
13+
// Test __builtin_ia32_sqrtps256 - single precision vector sqrt (256-bit)
14+
__m256 test_sqrtps256(__m256 x) {
15+
return __builtin_ia32_sqrtps256(x);
16+
}
17+
// CHECK-LABEL: cir.func @test_sqrtps256
18+
// CHECK: cir.sqrt
19+
20+
// Test __builtin_ia32_sqrtps512 - single precision vector sqrt (512-bit)
21+
__m512 test_sqrtps512(__m512 x) {
22+
return __builtin_ia32_sqrtps512(x);
23+
}
24+
// CHECK-LABEL: cir.func @test_sqrtps512
25+
// CHECK: cir.sqrt
26+
27+
// Test __builtin_ia32_sqrtpd - double precision vector sqrt (128-bit)
28+
__m128d test_sqrtpd(__m128d x) {
29+
return __builtin_ia32_sqrtpd(x);
30+
}
31+
// CHECK-LABEL: cir.func @test_sqrtpd
32+
// CHECK: cir.sqrt
33+
34+
// Test __builtin_ia32_sqrtpd256 - double precision vector sqrt (256-bit)
35+
__m256d test_sqrtpd256(__m256d x) {
36+
return __builtin_ia32_sqrtpd256(x);
37+
}
38+
// CHECK-LABEL: cir.func @test_sqrtpd256
39+
// CHECK: cir.sqrt
40+
41+
// Test __builtin_ia32_sqrtpd512 - double precision vector sqrt (512-bit)
42+
__m512d test_sqrtpd512(__m512d x) {
43+
return __builtin_ia32_sqrtpd512(x);
44+
}
45+
// CHECK-LABEL: cir.func @test_sqrtpd512
46+
// CHECK: cir.sqrt

clang/test/CIR/cir-sqrt-f32.mlir

Lines changed: 0 additions & 15 deletions
This file was deleted.

clang/test/CIR/cir-sqrt-f64.mlir

Lines changed: 0 additions & 12 deletions
This file was deleted.

clang/test/CIR/cir-sqrt-v4f32.mlir

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)