Skip to content

Commit 98e8811

Browse files
el-evlanza
authored andcommitted
[CIR][CIRGen][Builtin] Add several elementwise FP builtins (#1553)
Added: - `cos` - `floor` - `round` - `rint` - `nearbyint` - `sin` - `sqrt` - `tan` - `trunc`
1 parent 5987318 commit 98e8811

File tree

2 files changed

+199
-11
lines changed

2 files changed

+199
-11
lines changed

Diff for: clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,6 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
980980
assert(!cir::MissingFeatures::fastMathFlags());
981981
return emitUnaryMaybeConstrainedFPBuiltin<cir::SqrtOp>(*this, *E);
982982

983-
case Builtin::BI__builtin_elementwise_sqrt:
984-
llvm_unreachable("BI__builtin_elementwise_sqrt NYI");
985-
986983
case Builtin::BItan:
987984
case Builtin::BItanf:
988985
case Builtin::BItanl:
@@ -1520,31 +1517,33 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
15201517
case Builtin::BI__builtin_elementwise_bitreverse:
15211518
llvm_unreachable("BI__builtin_elementwise_bitreverse NYI");
15221519
case Builtin::BI__builtin_elementwise_cos:
1523-
llvm_unreachable("BI__builtin_elementwise_cos NYI");
1520+
return emitUnaryFPBuiltin<cir::CosOp>(*this, *E);
15241521
case Builtin::BI__builtin_elementwise_cosh:
15251522
llvm_unreachable("BI__builtin_elementwise_cosh NYI");
15261523
case Builtin::BI__builtin_elementwise_floor:
1527-
llvm_unreachable("BI__builtin_elementwise_floor NYI");
1524+
return emitUnaryFPBuiltin<cir::FloorOp>(*this, *E);
15281525
case Builtin::BI__builtin_elementwise_popcount:
15291526
llvm_unreachable("BI__builtin_elementwise_popcount NYI");
15301527
case Builtin::BI__builtin_elementwise_roundeven:
15311528
llvm_unreachable("BI__builtin_elementwise_roundeven NYI");
15321529
case Builtin::BI__builtin_elementwise_round:
1533-
llvm_unreachable("BI__builtin_elementwise_round NYI");
1530+
return emitUnaryFPBuiltin<cir::RoundOp>(*this, *E);
15341531
case Builtin::BI__builtin_elementwise_rint:
1535-
llvm_unreachable("BI__builtin_elementwise_rint NYI");
1532+
return emitUnaryFPBuiltin<cir::RintOp>(*this, *E);
15361533
case Builtin::BI__builtin_elementwise_nearbyint:
1537-
llvm_unreachable("BI__builtin_elementwise_nearbyint NYI");
1534+
return emitUnaryFPBuiltin<cir::NearbyintOp>(*this, *E);
15381535
case Builtin::BI__builtin_elementwise_sin:
1539-
llvm_unreachable("BI__builtin_elementwise_sin NYI");
1536+
return emitUnaryFPBuiltin<cir::SinOp>(*this, *E);
15401537
case Builtin::BI__builtin_elementwise_sinh:
15411538
llvm_unreachable("BI__builtin_elementwise_sinh NYI");
1539+
case Builtin::BI__builtin_elementwise_sqrt:
1540+
return emitUnaryFPBuiltin<cir::SqrtOp>(*this, *E);
15421541
case Builtin::BI__builtin_elementwise_tan:
1543-
llvm_unreachable("BI__builtin_elementwise_tan NYI");
1542+
return emitUnaryFPBuiltin<cir::TanOp>(*this, *E);
15441543
case Builtin::BI__builtin_elementwise_tanh:
15451544
llvm_unreachable("BI__builtin_elementwise_tanh NYI");
15461545
case Builtin::BI__builtin_elementwise_trunc:
1547-
llvm_unreachable("BI__builtin_elementwise_trunc NYI");
1546+
return emitUnaryFPBuiltin<cir::TruncOp>(*this, *E);
15481547
case Builtin::BI__builtin_elementwise_canonicalize:
15491548
llvm_unreachable("BI__builtin_elementwise_canonicalize NYI");
15501549
case Builtin::BI__builtin_elementwise_copysign:

Diff for: clang/test/CIR/CodeGen/builtins-elementwise.c

+189
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,192 @@ void test_builtin_elementwise_log10(float f, double d, vfloat4 vf4,
183183
// LLVM: {{%.*}} = call <4 x double> @llvm.log10.v4f64(<4 x double> {{%.*}})
184184
vd4 = __builtin_elementwise_log10(vd4);
185185
}
186+
187+
void test_builtin_elementwise_cos(float f, double d, vfloat4 vf4,
188+
vdouble4 vd4) {
189+
// CIR-LABEL: test_builtin_elementwise_cos
190+
// LLVM-LABEL: test_builtin_elementwise_cos
191+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.float
192+
// LLVM: {{%.*}} = call float @llvm.cos.f32(float {{%.*}})
193+
f = __builtin_elementwise_cos(f);
194+
195+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.double
196+
// LLVM: {{%.*}} = call double @llvm.cos.f64(double {{%.*}})
197+
d = __builtin_elementwise_cos(d);
198+
199+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.vector<!cir.float x 4>
200+
// LLVM: {{%.*}} = call <4 x float> @llvm.cos.v4f32(<4 x float> {{%.*}})
201+
vf4 = __builtin_elementwise_cos(vf4);
202+
203+
// CIR: {{%.*}} = cir.cos {{%.*}} : !cir.vector<!cir.double x 4>
204+
// LLVM: {{%.*}} = call <4 x double> @llvm.cos.v4f64(<4 x double> {{%.*}})
205+
vd4 = __builtin_elementwise_cos(vd4);
206+
}
207+
208+
void test_builtin_elementwise_floor(float f, double d, vfloat4 vf4,
209+
vdouble4 vd4) {
210+
// CIR-LABEL: test_builtin_elementwise_floor
211+
// LLVM-LABEL: test_builtin_elementwise_floor
212+
// CIR: {{%.*}} = cir.floor {{%.*}} : !cir.float
213+
// LLVM: {{%.*}} = call float @llvm.floor.f32(float {{%.*}})
214+
f = __builtin_elementwise_floor(f);
215+
216+
// CIR: {{%.*}} = cir.floor {{%.*}} : !cir.double
217+
// LLVM: {{%.*}} = call double @llvm.floor.f64(double {{%.*}})
218+
d = __builtin_elementwise_floor(d);
219+
220+
// CIR: {{%.*}} = cir.floor {{%.*}} : !cir.vector<!cir.float x 4>
221+
// LLVM: {{%.*}} = call <4 x float> @llvm.floor.v4f32(<4 x float> {{%.*}})
222+
vf4 = __builtin_elementwise_floor(vf4);
223+
224+
// CIR: {{%.*}} = cir.floor {{%.*}} : !cir.vector<!cir.double x 4>
225+
// LLVM: {{%.*}} = call <4 x double> @llvm.floor.v4f64(<4 x double> {{%.*}})
226+
vd4 = __builtin_elementwise_floor(vd4);
227+
}
228+
229+
void test_builtin_elementwise_round(float f, double d, vfloat4 vf4,
230+
vdouble4 vd4) {
231+
// CIR-LABEL: test_builtin_elementwise_round
232+
// LLVM-LABEL: test_builtin_elementwise_round
233+
// CIR: {{%.*}} = cir.round {{%.*}} : !cir.float
234+
// LLVM: {{%.*}} = call float @llvm.round.f32(float {{%.*}})
235+
f = __builtin_elementwise_round(f);
236+
237+
// CIR: {{%.*}} = cir.round {{%.*}} : !cir.double
238+
// LLVM: {{%.*}} = call double @llvm.round.f64(double {{%.*}})
239+
d = __builtin_elementwise_round(d);
240+
241+
// CIR: {{%.*}} = cir.round {{%.*}} : !cir.vector<!cir.float x 4>
242+
// LLVM: {{%.*}} = call <4 x float> @llvm.round.v4f32(<4 x float> {{%.*}})
243+
vf4 = __builtin_elementwise_round(vf4);
244+
245+
// CIR: {{%.*}} = cir.round {{%.*}} : !cir.vector<!cir.double x 4>
246+
// LLVM: {{%.*}} = call <4 x double> @llvm.round.v4f64(<4 x double> {{%.*}})
247+
vd4 = __builtin_elementwise_round(vd4);
248+
}
249+
250+
void test_builtin_elementwise_rint(float f, double d, vfloat4 vf4,
251+
vdouble4 vd4) {
252+
// CIR-LABEL: test_builtin_elementwise_rint
253+
// LLVM-LABEL: test_builtin_elementwise_rint
254+
// CIR: {{%.*}} = cir.rint {{%.*}} : !cir.float
255+
// LLVM: {{%.*}} = call float @llvm.rint.f32(float {{%.*}})
256+
f = __builtin_elementwise_rint(f);
257+
258+
// CIR: {{%.*}} = cir.rint {{%.*}} : !cir.double
259+
// LLVM: {{%.*}} = call double @llvm.rint.f64(double {{%.*}})
260+
d = __builtin_elementwise_rint(d);
261+
262+
// CIR: {{%.*}} = cir.rint {{%.*}} : !cir.vector<!cir.float x 4>
263+
// LLVM: {{%.*}} = call <4 x float> @llvm.rint.v4f32(<4 x float> {{%.*}})
264+
vf4 = __builtin_elementwise_rint(vf4);
265+
266+
// CIR: {{%.*}} = cir.rint {{%.*}} : !cir.vector<!cir.double x 4>
267+
// LLVM: {{%.*}} = call <4 x double> @llvm.rint.v4f64(<4 x double> {{%.*}})
268+
vd4 = __builtin_elementwise_rint(vd4);
269+
}
270+
271+
void test_builtin_elementwise_nearbyint(float f, double d, vfloat4 vf4,
272+
vdouble4 vd4) {
273+
// CIR-LABEL: test_builtin_elementwise_nearbyint
274+
// LLVM-LABEL: test_builtin_elementwise_nearbyint
275+
// CIR: {{%.*}} = cir.nearbyint {{%.*}} : !cir.float
276+
// LLVM: {{%.*}} = call float @llvm.nearbyint.f32(float {{%.*}})
277+
f = __builtin_elementwise_nearbyint(f);
278+
279+
// CIR: {{%.*}} = cir.nearbyint {{%.*}} : !cir.double
280+
// LLVM: {{%.*}} = call double @llvm.nearbyint.f64(double {{%.*}})
281+
d = __builtin_elementwise_nearbyint(d);
282+
283+
// CIR: {{%.*}} = cir.nearbyint {{%.*}} : !cir.vector<!cir.float x 4>
284+
// LLVM: {{%.*}} = call <4 x float> @llvm.nearbyint.v4f32(<4 x float> {{%.*}})
285+
vf4 = __builtin_elementwise_nearbyint(vf4);
286+
287+
// CIR: {{%.*}} = cir.nearbyint {{%.*}} : !cir.vector<!cir.double x 4>
288+
// LLVM: {{%.*}} = call <4 x double> @llvm.nearbyint.v4f64(<4 x double> {{%.*}})
289+
vd4 = __builtin_elementwise_nearbyint(vd4);
290+
}
291+
292+
void test_builtin_elementwise_sin(float f, double d, vfloat4 vf4,
293+
vdouble4 vd4) {
294+
// CIR-LABEL: test_builtin_elementwise_sin
295+
// LLVM-LABEL: test_builtin_elementwise_sin
296+
// CIR: {{%.*}} = cir.sin {{%.*}} : !cir.float
297+
// LLVM: {{%.*}} = call float @llvm.sin.f32(float {{%.*}})
298+
f = __builtin_elementwise_sin(f);
299+
300+
// CIR: {{%.*}} = cir.sin {{%.*}} : !cir.double
301+
// LLVM: {{%.*}} = call double @llvm.sin.f64(double {{%.*}})
302+
d = __builtin_elementwise_sin(d);
303+
304+
// CIR: {{%.*}} = cir.sin {{%.*}} : !cir.vector<!cir.float x 4>
305+
// LLVM: {{%.*}} = call <4 x float> @llvm.sin.v4f32(<4 x float> {{%.*}})
306+
vf4 = __builtin_elementwise_sin(vf4);
307+
308+
// CIR: {{%.*}} = cir.sin {{%.*}} : !cir.vector<!cir.double x 4>
309+
// LLVM: {{%.*}} = call <4 x double> @llvm.sin.v4f64(<4 x double> {{%.*}})
310+
vd4 = __builtin_elementwise_sin(vd4);
311+
}
312+
313+
void test_builtin_elementwise_sqrt(float f, double d, vfloat4 vf4,
314+
vdouble4 vd4) {
315+
// CIR-LABEL: test_builtin_elementwise_sqrt
316+
// LLVM-LABEL: test_builtin_elementwise_sqrt
317+
// CIR: {{%.*}} = cir.sqrt {{%.*}} : !cir.float
318+
// LLVM: {{%.*}} = call float @llvm.sqrt.f32(float {{%.*}})
319+
f = __builtin_elementwise_sqrt(f);
320+
321+
// CIR: {{%.*}} = cir.sqrt {{%.*}} : !cir.double
322+
// LLVM: {{%.*}} = call double @llvm.sqrt.f64(double {{%.*}})
323+
d = __builtin_elementwise_sqrt(d);
324+
325+
// CIR: {{%.*}} = cir.sqrt {{%.*}} : !cir.vector<!cir.float x 4>
326+
// LLVM: {{%.*}} = call <4 x float> @llvm.sqrt.v4f32(<4 x float> {{%.*}})
327+
vf4 = __builtin_elementwise_sqrt(vf4);
328+
329+
// CIR: {{%.*}} = cir.sqrt {{%.*}} : !cir.vector<!cir.double x 4>
330+
// LLVM: {{%.*}} = call <4 x double> @llvm.sqrt.v4f64(<4 x double> {{%.*}})
331+
vd4 = __builtin_elementwise_sqrt(vd4);
332+
}
333+
334+
void test_builtin_elementwise_tan(float f, double d, vfloat4 vf4,
335+
vdouble4 vd4) {
336+
// CIR-LABEL: test_builtin_elementwise_tan
337+
// LLVM-LABEL: test_builtin_elementwise_tan
338+
// CIR: {{%.*}} = cir.tan {{%.*}} : !cir.float
339+
// LLVM: {{%.*}} = call float @llvm.tan.f32(float {{%.*}})
340+
f = __builtin_elementwise_tan(f);
341+
342+
// CIR: {{%.*}} = cir.tan {{%.*}} : !cir.double
343+
// LLVM: {{%.*}} = call double @llvm.tan.f64(double {{%.*}})
344+
d = __builtin_elementwise_tan(d);
345+
346+
// CIR: {{%.*}} = cir.tan {{%.*}} : !cir.vector<!cir.float x 4>
347+
// LLVM: {{%.*}} = call <4 x float> @llvm.tan.v4f32(<4 x float> {{%.*}})
348+
vf4 = __builtin_elementwise_tan(vf4);
349+
350+
// CIR: {{%.*}} = cir.tan {{%.*}} : !cir.vector<!cir.double x 4>
351+
// LLVM: {{%.*}} = call <4 x double> @llvm.tan.v4f64(<4 x double> {{%.*}})
352+
vd4 = __builtin_elementwise_tan(vd4);
353+
}
354+
355+
void test_builtin_elementwise_trunc(float f, double d, vfloat4 vf4,
356+
vdouble4 vd4) {
357+
// CIR-LABEL: test_builtin_elementwise_trunc
358+
// LLVM-LABEL: test_builtin_elementwise_trunc
359+
// CIR: {{%.*}} = cir.trunc {{%.*}} : !cir.float
360+
// LLVM: {{%.*}} = call float @llvm.trunc.f32(float {{%.*}})
361+
f = __builtin_elementwise_trunc(f);
362+
363+
// CIR: {{%.*}} = cir.trunc {{%.*}} : !cir.double
364+
// LLVM: {{%.*}} = call double @llvm.trunc.f64(double {{%.*}})
365+
d = __builtin_elementwise_trunc(d);
366+
367+
// CIR: {{%.*}} = cir.trunc {{%.*}} : !cir.vector<!cir.float x 4>
368+
// LLVM: {{%.*}} = call <4 x float> @llvm.trunc.v4f32(<4 x float> {{%.*}})
369+
vf4 = __builtin_elementwise_trunc(vf4);
370+
371+
// CIR: {{%.*}} = cir.trunc {{%.*}} : !cir.vector<!cir.double x 4>
372+
// LLVM: {{%.*}} = call <4 x double> @llvm.trunc.v4f64(<4 x double> {{%.*}})
373+
vd4 = __builtin_elementwise_trunc(vd4);
374+
}

0 commit comments

Comments
 (0)