clang 22.0.0git
AMDGPU.cpp
Go to the documentation of this file.
1//===------- AMDCPU.cpp - Emit LLVM Code for builtins ---------------------===//
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 contains code to emit Builtin calls as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGBuiltin.h"
15#include "llvm/Analysis/ValueTracking.h"
16#include "llvm/IR/IntrinsicsAMDGPU.h"
17#include "llvm/IR/IntrinsicsR600.h"
18#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
19#include "llvm/Support/AMDGPUAddrSpace.h"
20
21using namespace clang;
22using namespace CodeGen;
23using namespace llvm;
24
25namespace {
26
27// Has second type mangled argument.
28static Value *
30 Intrinsic::ID IntrinsicID,
31 Intrinsic::ID ConstrainedIntrinsicID) {
32 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
33 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
34
35 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
36 if (CGF.Builder.getIsFPConstrained()) {
37 Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID,
38 {Src0->getType(), Src1->getType()});
39 return CGF.Builder.CreateConstrainedFPCall(F, {Src0, Src1});
40 }
41
42 Function *F =
43 CGF.CGM.getIntrinsic(IntrinsicID, {Src0->getType(), Src1->getType()});
44 return CGF.Builder.CreateCall(F, {Src0, Src1});
45}
46
47// If \p E is not null pointer, insert address space cast to match return
48// type of \p E if necessary.
49Value *EmitAMDGPUDispatchPtr(CodeGenFunction &CGF,
50 const CallExpr *E = nullptr) {
51 auto *F = CGF.CGM.getIntrinsic(Intrinsic::amdgcn_dispatch_ptr);
52 auto *Call = CGF.Builder.CreateCall(F);
53 Call->addRetAttr(
54 Attribute::getWithDereferenceableBytes(Call->getContext(), 64));
55 Call->addRetAttr(Attribute::getWithAlignment(Call->getContext(), Align(4)));
56 if (!E)
57 return Call;
58 QualType BuiltinRetType = E->getType();
59 auto *RetTy = cast<llvm::PointerType>(CGF.ConvertType(BuiltinRetType));
60 if (RetTy == Call->getType())
61 return Call;
62 return CGF.Builder.CreateAddrSpaceCast(Call, RetTy);
63}
64
65Value *EmitAMDGPUImplicitArgPtr(CodeGenFunction &CGF) {
66 auto *F = CGF.CGM.getIntrinsic(Intrinsic::amdgcn_implicitarg_ptr);
67 auto *Call = CGF.Builder.CreateCall(F);
68 Call->addRetAttr(
69 Attribute::getWithDereferenceableBytes(Call->getContext(), 256));
70 Call->addRetAttr(Attribute::getWithAlignment(Call->getContext(), Align(8)));
71 return Call;
72}
73
74// \p Index is 0, 1, and 2 for x, y, and z dimension, respectively.
75/// Emit code based on Code Object ABI version.
76/// COV_4 : Emit code to use dispatch ptr
77/// COV_5+ : Emit code to use implicitarg ptr
78/// COV_NONE : Emit code to load a global variable "__oclc_ABI_version"
79/// and use its value for COV_4 or COV_5+ approach. It is used for
80/// compiling device libraries in an ABI-agnostic way.
81Value *EmitAMDGPUWorkGroupSize(CodeGenFunction &CGF, unsigned Index) {
82 llvm::LoadInst *LD;
83
84 auto Cov = CGF.getTarget().getTargetOpts().CodeObjectVersion;
85
86 if (Cov == CodeObjectVersionKind::COV_None) {
87 StringRef Name = "__oclc_ABI_version";
88 auto *ABIVersionC = CGF.CGM.getModule().getNamedGlobal(Name);
89 if (!ABIVersionC)
90 ABIVersionC = new llvm::GlobalVariable(
91 CGF.CGM.getModule(), CGF.Int32Ty, false,
92 llvm::GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
93 llvm::GlobalVariable::NotThreadLocal,
95
96 // This load will be eliminated by the IPSCCP because it is constant
97 // weak_odr without externally_initialized. Either changing it to weak or
98 // adding externally_initialized will keep the load.
99 Value *ABIVersion = CGF.Builder.CreateAlignedLoad(CGF.Int32Ty, ABIVersionC,
100 CGF.CGM.getIntAlign());
101
102 Value *IsCOV5 = CGF.Builder.CreateICmpSGE(
103 ABIVersion,
104 llvm::ConstantInt::get(CGF.Int32Ty, CodeObjectVersionKind::COV_5));
105
106 // Indexing the implicit kernarg segment.
107 Value *ImplicitGEP = CGF.Builder.CreateConstGEP1_32(
108 CGF.Int8Ty, EmitAMDGPUImplicitArgPtr(CGF), 12 + Index * 2);
109
110 // Indexing the HSA kernel_dispatch_packet struct.
111 Value *DispatchGEP = CGF.Builder.CreateConstGEP1_32(
112 CGF.Int8Ty, EmitAMDGPUDispatchPtr(CGF), 4 + Index * 2);
113
114 auto Result = CGF.Builder.CreateSelect(IsCOV5, ImplicitGEP, DispatchGEP);
115 LD = CGF.Builder.CreateLoad(
116 Address(Result, CGF.Int16Ty, CharUnits::fromQuantity(2)));
117 } else {
118 Value *GEP = nullptr;
119 if (Cov >= CodeObjectVersionKind::COV_5) {
120 // Indexing the implicit kernarg segment.
121 GEP = CGF.Builder.CreateConstGEP1_32(
122 CGF.Int8Ty, EmitAMDGPUImplicitArgPtr(CGF), 12 + Index * 2);
123 } else {
124 // Indexing the HSA kernel_dispatch_packet struct.
125 GEP = CGF.Builder.CreateConstGEP1_32(
126 CGF.Int8Ty, EmitAMDGPUDispatchPtr(CGF), 4 + Index * 2);
127 }
128 LD = CGF.Builder.CreateLoad(
130 }
131
132 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
133 llvm::MDNode *RNode = MDHelper.createRange(APInt(16, 1),
134 APInt(16, CGF.getTarget().getMaxOpenCLWorkGroupSize() + 1));
135 LD->setMetadata(llvm::LLVMContext::MD_range, RNode);
136 LD->setMetadata(llvm::LLVMContext::MD_noundef,
137 llvm::MDNode::get(CGF.getLLVMContext(), {}));
138 LD->setMetadata(llvm::LLVMContext::MD_invariant_load,
139 llvm::MDNode::get(CGF.getLLVMContext(), {}));
140 return LD;
141}
142
143// \p Index is 0, 1, and 2 for x, y, and z dimension, respectively.
144Value *EmitAMDGPUGridSize(CodeGenFunction &CGF, unsigned Index) {
145 const unsigned XOffset = 12;
146 auto *DP = EmitAMDGPUDispatchPtr(CGF);
147 // Indexing the HSA kernel_dispatch_packet struct.
148 auto *Offset = llvm::ConstantInt::get(CGF.Int32Ty, XOffset + Index * 4);
149 auto *GEP = CGF.Builder.CreateGEP(CGF.Int8Ty, DP, Offset);
150 auto *LD = CGF.Builder.CreateLoad(
152
153 llvm::MDBuilder MDB(CGF.getLLVMContext());
154
155 // Known non-zero.
156 LD->setMetadata(llvm::LLVMContext::MD_range,
157 MDB.createRange(APInt(32, 1), APInt::getZero(32)));
158 LD->setMetadata(llvm::LLVMContext::MD_invariant_load,
159 llvm::MDNode::get(CGF.getLLVMContext(), {}));
160 return LD;
161}
162} // namespace
163
164// Generates the IR for __builtin_read_exec_*.
165// Lowers the builtin to amdgcn_ballot intrinsic.
167 llvm::Type *RegisterType,
168 llvm::Type *ValueType, bool isExecHi) {
169 CodeGen::CGBuilderTy &Builder = CGF.Builder;
170 CodeGen::CodeGenModule &CGM = CGF.CGM;
171
172 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_ballot, {RegisterType});
173 llvm::Value *Call = Builder.CreateCall(F, {Builder.getInt1(true)});
174
175 if (isExecHi) {
176 Value *Rt2 = Builder.CreateLShr(Call, 32);
177 Rt2 = Builder.CreateTrunc(Rt2, CGF.Int32Ty);
178 return Rt2;
179 }
180
181 return Call;
182}
183
184// Emit an intrinsic that has 1 float or double operand, and 1 integer.
186 const CallExpr *E,
187 unsigned IntrinsicID) {
188 llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
189 llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
190
191 Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
192 return CGF.Builder.CreateCall(F, {Src0, Src1});
193}
194
195// For processing memory ordering and memory scope arguments of various
196// amdgcn builtins.
197// \p Order takes a C++11 comptabile memory-ordering specifier and converts
198// it into LLVM's memory ordering specifier using atomic C ABI, and writes
199// to \p AO. \p Scope takes a const char * and converts it into AMDGCN
200// specific SyncScopeID and writes it to \p SSID.
202 llvm::AtomicOrdering &AO,
203 llvm::SyncScope::ID &SSID) {
204 int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
205
206 // Map C11/C++11 memory ordering to LLVM memory ordering
207 assert(llvm::isValidAtomicOrderingCABI(ord));
208 switch (static_cast<llvm::AtomicOrderingCABI>(ord)) {
209 case llvm::AtomicOrderingCABI::acquire:
210 case llvm::AtomicOrderingCABI::consume:
211 AO = llvm::AtomicOrdering::Acquire;
212 break;
213 case llvm::AtomicOrderingCABI::release:
214 AO = llvm::AtomicOrdering::Release;
215 break;
216 case llvm::AtomicOrderingCABI::acq_rel:
217 AO = llvm::AtomicOrdering::AcquireRelease;
218 break;
219 case llvm::AtomicOrderingCABI::seq_cst:
220 AO = llvm::AtomicOrdering::SequentiallyConsistent;
221 break;
222 case llvm::AtomicOrderingCABI::relaxed:
223 AO = llvm::AtomicOrdering::Monotonic;
224 break;
225 }
226
227 // Some of the atomic builtins take the scope as a string name.
228 StringRef scp;
229 if (llvm::getConstantStringInfo(Scope, scp)) {
230 SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
231 return;
232 }
233
234 // Older builtins had an enum argument for the memory scope.
235 int scope = cast<llvm::ConstantInt>(Scope)->getZExtValue();
236 switch (scope) {
237 case 0: // __MEMORY_SCOPE_SYSTEM
238 SSID = llvm::SyncScope::System;
239 break;
240 case 1: // __MEMORY_SCOPE_DEVICE
241 SSID = getLLVMContext().getOrInsertSyncScopeID("agent");
242 break;
243 case 2: // __MEMORY_SCOPE_WRKGRP
244 SSID = getLLVMContext().getOrInsertSyncScopeID("workgroup");
245 break;
246 case 3: // __MEMORY_SCOPE_WVFRNT
247 SSID = getLLVMContext().getOrInsertSyncScopeID("wavefront");
248 break;
249 case 4: // __MEMORY_SCOPE_SINGLE
250 SSID = llvm::SyncScope::SingleThread;
251 break;
252 default:
253 SSID = llvm::SyncScope::System;
254 break;
255 }
256}
257
258llvm::Value *CodeGenFunction::EmitScalarOrConstFoldImmArg(unsigned ICEArguments,
259 unsigned Idx,
260 const CallExpr *E) {
261 llvm::Value *Arg = nullptr;
262 if ((ICEArguments & (1 << Idx)) == 0) {
263 Arg = EmitScalarExpr(E->getArg(Idx));
264 } else {
265 // If this is required to be a constant, constant fold it so that we
266 // know that the generated intrinsic gets a ConstantInt.
267 std::optional<llvm::APSInt> Result =
269 assert(Result && "Expected argument to be a constant");
270 Arg = llvm::ConstantInt::get(getLLVMContext(), *Result);
271 }
272 return Arg;
273}
274
276 const CallExpr *E) {
277 constexpr const char *Tag = "amdgpu-synchronize-as";
278
279 LLVMContext &Ctx = Inst->getContext();
281 for (unsigned K = 2; K < E->getNumArgs(); ++K) {
282 llvm::Value *V = EmitScalarExpr(E->getArg(K));
283 StringRef AS;
284 if (llvm::getConstantStringInfo(V, AS)) {
285 MMRAs.push_back({Tag, AS});
286 // TODO: Delete the resulting unused constant?
287 continue;
288 }
289 CGM.Error(E->getExprLoc(),
290 "expected an address space name as a string literal");
291 }
292
293 llvm::sort(MMRAs);
294 MMRAs.erase(llvm::unique(MMRAs), MMRAs.end());
295 Inst->setMetadata(LLVMContext::MD_mmra, MMRAMetadata::getMD(Ctx, MMRAs));
296}
297
298static Intrinsic::ID getIntrinsicIDforWaveReduction(unsigned BuiltinID) {
299 switch (BuiltinID) {
300 default:
301 llvm_unreachable("Unknown BuiltinID for wave reduction");
302 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_add_u32:
303 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_add_u64:
304 return Intrinsic::amdgcn_wave_reduce_add;
305 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_sub_u32:
306 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_sub_u64:
307 return Intrinsic::amdgcn_wave_reduce_sub;
308 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_min_i32:
309 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_min_i64:
310 return Intrinsic::amdgcn_wave_reduce_min;
311 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_min_u32:
312 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_min_u64:
313 return Intrinsic::amdgcn_wave_reduce_umin;
314 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_max_i32:
315 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_max_i64:
316 return Intrinsic::amdgcn_wave_reduce_max;
317 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_max_u32:
318 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_max_u64:
319 return Intrinsic::amdgcn_wave_reduce_umax;
320 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_and_b32:
321 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_and_b64:
322 return Intrinsic::amdgcn_wave_reduce_and;
323 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_or_b32:
324 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_or_b64:
325 return Intrinsic::amdgcn_wave_reduce_or;
326 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_xor_b32:
327 case clang::AMDGPU::BI__builtin_amdgcn_wave_reduce_xor_b64:
328 return Intrinsic::amdgcn_wave_reduce_xor;
329 }
330}
331
333 const CallExpr *E) {
334 llvm::AtomicOrdering AO = llvm::AtomicOrdering::SequentiallyConsistent;
335 llvm::SyncScope::ID SSID;
336 switch (BuiltinID) {
337 case AMDGPU::BI__builtin_amdgcn_wave_reduce_add_u32:
338 case AMDGPU::BI__builtin_amdgcn_wave_reduce_sub_u32:
339 case AMDGPU::BI__builtin_amdgcn_wave_reduce_min_i32:
340 case AMDGPU::BI__builtin_amdgcn_wave_reduce_min_u32:
341 case AMDGPU::BI__builtin_amdgcn_wave_reduce_max_i32:
342 case AMDGPU::BI__builtin_amdgcn_wave_reduce_max_u32:
343 case AMDGPU::BI__builtin_amdgcn_wave_reduce_and_b32:
344 case AMDGPU::BI__builtin_amdgcn_wave_reduce_or_b32:
345 case AMDGPU::BI__builtin_amdgcn_wave_reduce_xor_b32:
346 case AMDGPU::BI__builtin_amdgcn_wave_reduce_add_u64:
347 case AMDGPU::BI__builtin_amdgcn_wave_reduce_sub_u64:
348 case AMDGPU::BI__builtin_amdgcn_wave_reduce_min_i64:
349 case AMDGPU::BI__builtin_amdgcn_wave_reduce_min_u64:
350 case AMDGPU::BI__builtin_amdgcn_wave_reduce_max_i64:
351 case AMDGPU::BI__builtin_amdgcn_wave_reduce_max_u64:
352 case AMDGPU::BI__builtin_amdgcn_wave_reduce_and_b64:
353 case AMDGPU::BI__builtin_amdgcn_wave_reduce_or_b64:
354 case AMDGPU::BI__builtin_amdgcn_wave_reduce_xor_b64: {
355 Intrinsic::ID IID = getIntrinsicIDforWaveReduction(BuiltinID);
356 llvm::Value *Value = EmitScalarExpr(E->getArg(0));
357 llvm::Value *Strategy = EmitScalarExpr(E->getArg(1));
358 llvm::Function *F = CGM.getIntrinsic(IID, {Value->getType()});
359 return Builder.CreateCall(F, {Value, Strategy});
360 }
361 case AMDGPU::BI__builtin_amdgcn_div_scale:
362 case AMDGPU::BI__builtin_amdgcn_div_scalef: {
363 // Translate from the intrinsics's struct return to the builtin's out
364 // argument.
365
366 Address FlagOutPtr = EmitPointerWithAlignment(E->getArg(3));
367
368 llvm::Value *X = EmitScalarExpr(E->getArg(0));
369 llvm::Value *Y = EmitScalarExpr(E->getArg(1));
370 llvm::Value *Z = EmitScalarExpr(E->getArg(2));
371
372 llvm::Function *Callee = CGM.getIntrinsic(Intrinsic::amdgcn_div_scale,
373 X->getType());
374
375 llvm::Value *Tmp = Builder.CreateCall(Callee, {X, Y, Z});
376
377 llvm::Value *Result = Builder.CreateExtractValue(Tmp, 0);
378 llvm::Value *Flag = Builder.CreateExtractValue(Tmp, 1);
379
380 llvm::Type *RealFlagType = FlagOutPtr.getElementType();
381
382 llvm::Value *FlagExt = Builder.CreateZExt(Flag, RealFlagType);
383 Builder.CreateStore(FlagExt, FlagOutPtr);
384 return Result;
385 }
386 case AMDGPU::BI__builtin_amdgcn_div_fmas:
387 case AMDGPU::BI__builtin_amdgcn_div_fmasf: {
388 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
389 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
390 llvm::Value *Src2 = EmitScalarExpr(E->getArg(2));
391 llvm::Value *Src3 = EmitScalarExpr(E->getArg(3));
392
393 llvm::Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_div_fmas,
394 Src0->getType());
395 llvm::Value *Src3ToBool = Builder.CreateIsNotNull(Src3);
396 return Builder.CreateCall(F, {Src0, Src1, Src2, Src3ToBool});
397 }
398
399 case AMDGPU::BI__builtin_amdgcn_ds_swizzle:
401 Intrinsic::amdgcn_ds_swizzle);
402 case AMDGPU::BI__builtin_amdgcn_mov_dpp8:
403 case AMDGPU::BI__builtin_amdgcn_mov_dpp:
404 case AMDGPU::BI__builtin_amdgcn_update_dpp: {
406 // Find out if any arguments are required to be integer constant
407 // expressions.
408 unsigned ICEArguments = 0;
410 getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments);
411 assert(Error == ASTContext::GE_None && "Should not codegen an error");
412 llvm::Type *DataTy = ConvertType(E->getArg(0)->getType());
413 unsigned Size = DataTy->getPrimitiveSizeInBits();
414 llvm::Type *IntTy =
415 llvm::IntegerType::get(Builder.getContext(), std::max(Size, 32u));
416 Function *F =
417 CGM.getIntrinsic(BuiltinID == AMDGPU::BI__builtin_amdgcn_mov_dpp8
418 ? Intrinsic::amdgcn_mov_dpp8
419 : Intrinsic::amdgcn_update_dpp,
420 IntTy);
421 assert(E->getNumArgs() == 5 || E->getNumArgs() == 6 ||
422 E->getNumArgs() == 2);
423 bool InsertOld = BuiltinID == AMDGPU::BI__builtin_amdgcn_mov_dpp;
424 if (InsertOld)
425 Args.push_back(llvm::PoisonValue::get(IntTy));
426 for (unsigned I = 0; I != E->getNumArgs(); ++I) {
427 llvm::Value *V = EmitScalarOrConstFoldImmArg(ICEArguments, I, E);
428 if (I < (BuiltinID == AMDGPU::BI__builtin_amdgcn_update_dpp ? 2u : 1u) &&
429 Size < 32) {
430 if (!DataTy->isIntegerTy())
431 V = Builder.CreateBitCast(
432 V, llvm::IntegerType::get(Builder.getContext(), Size));
433 V = Builder.CreateZExtOrBitCast(V, IntTy);
434 }
435 llvm::Type *ExpTy =
436 F->getFunctionType()->getFunctionParamType(I + InsertOld);
437 Args.push_back(Builder.CreateTruncOrBitCast(V, ExpTy));
438 }
439 Value *V = Builder.CreateCall(F, Args);
440 if (Size < 32 && !DataTy->isIntegerTy())
441 V = Builder.CreateTrunc(
442 V, llvm::IntegerType::get(Builder.getContext(), Size));
443 return Builder.CreateTruncOrBitCast(V, DataTy);
444 }
445 case AMDGPU::BI__builtin_amdgcn_permlane16:
446 case AMDGPU::BI__builtin_amdgcn_permlanex16:
448 *this, E,
449 BuiltinID == AMDGPU::BI__builtin_amdgcn_permlane16
450 ? Intrinsic::amdgcn_permlane16
451 : Intrinsic::amdgcn_permlanex16);
452 case AMDGPU::BI__builtin_amdgcn_permlane64:
454 Intrinsic::amdgcn_permlane64);
455 case AMDGPU::BI__builtin_amdgcn_readlane:
457 Intrinsic::amdgcn_readlane);
458 case AMDGPU::BI__builtin_amdgcn_readfirstlane:
460 Intrinsic::amdgcn_readfirstlane);
461 case AMDGPU::BI__builtin_amdgcn_div_fixup:
462 case AMDGPU::BI__builtin_amdgcn_div_fixupf:
463 case AMDGPU::BI__builtin_amdgcn_div_fixuph:
465 Intrinsic::amdgcn_div_fixup);
466 case AMDGPU::BI__builtin_amdgcn_trig_preop:
467 case AMDGPU::BI__builtin_amdgcn_trig_preopf:
468 return emitFPIntBuiltin(*this, E, Intrinsic::amdgcn_trig_preop);
469 case AMDGPU::BI__builtin_amdgcn_rcp:
470 case AMDGPU::BI__builtin_amdgcn_rcpf:
471 case AMDGPU::BI__builtin_amdgcn_rcph:
472 case AMDGPU::BI__builtin_amdgcn_rcp_bf16:
473 return emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::amdgcn_rcp);
474 case AMDGPU::BI__builtin_amdgcn_sqrt:
475 case AMDGPU::BI__builtin_amdgcn_sqrtf:
476 case AMDGPU::BI__builtin_amdgcn_sqrth:
477 case AMDGPU::BI__builtin_amdgcn_sqrt_bf16:
479 Intrinsic::amdgcn_sqrt);
480 case AMDGPU::BI__builtin_amdgcn_rsq:
481 case AMDGPU::BI__builtin_amdgcn_rsqf:
482 case AMDGPU::BI__builtin_amdgcn_rsqh:
483 case AMDGPU::BI__builtin_amdgcn_rsq_bf16:
484 return emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::amdgcn_rsq);
485 case AMDGPU::BI__builtin_amdgcn_rsq_clamp:
486 case AMDGPU::BI__builtin_amdgcn_rsq_clampf:
488 Intrinsic::amdgcn_rsq_clamp);
489 case AMDGPU::BI__builtin_amdgcn_sinf:
490 case AMDGPU::BI__builtin_amdgcn_sinh:
491 case AMDGPU::BI__builtin_amdgcn_sin_bf16:
492 return emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::amdgcn_sin);
493 case AMDGPU::BI__builtin_amdgcn_cosf:
494 case AMDGPU::BI__builtin_amdgcn_cosh:
495 case AMDGPU::BI__builtin_amdgcn_cos_bf16:
496 return emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::amdgcn_cos);
497 case AMDGPU::BI__builtin_amdgcn_dispatch_ptr:
498 return EmitAMDGPUDispatchPtr(*this, E);
499 case AMDGPU::BI__builtin_amdgcn_logf:
500 case AMDGPU::BI__builtin_amdgcn_log_bf16:
501 return emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::amdgcn_log);
502 case AMDGPU::BI__builtin_amdgcn_exp2f:
503 case AMDGPU::BI__builtin_amdgcn_exp2_bf16:
505 Intrinsic::amdgcn_exp2);
506 case AMDGPU::BI__builtin_amdgcn_log_clampf:
508 Intrinsic::amdgcn_log_clamp);
509 case AMDGPU::BI__builtin_amdgcn_ldexp:
510 case AMDGPU::BI__builtin_amdgcn_ldexpf: {
511 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
512 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
513 llvm::Function *F =
514 CGM.getIntrinsic(Intrinsic::ldexp, {Src0->getType(), Src1->getType()});
515 return Builder.CreateCall(F, {Src0, Src1});
516 }
517 case AMDGPU::BI__builtin_amdgcn_ldexph: {
518 // The raw instruction has a different behavior for out of bounds exponent
519 // values (implicit truncation instead of saturate to short_min/short_max).
520 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
521 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
522 llvm::Function *F =
523 CGM.getIntrinsic(Intrinsic::ldexp, {Src0->getType(), Int16Ty});
524 return Builder.CreateCall(F, {Src0, Builder.CreateTrunc(Src1, Int16Ty)});
525 }
526 case AMDGPU::BI__builtin_amdgcn_frexp_mant:
527 case AMDGPU::BI__builtin_amdgcn_frexp_mantf:
528 case AMDGPU::BI__builtin_amdgcn_frexp_manth:
530 Intrinsic::amdgcn_frexp_mant);
531 case AMDGPU::BI__builtin_amdgcn_frexp_exp:
532 case AMDGPU::BI__builtin_amdgcn_frexp_expf: {
533 Value *Src0 = EmitScalarExpr(E->getArg(0));
534 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_frexp_exp,
535 { Builder.getInt32Ty(), Src0->getType() });
536 return Builder.CreateCall(F, Src0);
537 }
538 case AMDGPU::BI__builtin_amdgcn_frexp_exph: {
539 Value *Src0 = EmitScalarExpr(E->getArg(0));
540 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_frexp_exp,
541 { Builder.getInt16Ty(), Src0->getType() });
542 return Builder.CreateCall(F, Src0);
543 }
544 case AMDGPU::BI__builtin_amdgcn_fract:
545 case AMDGPU::BI__builtin_amdgcn_fractf:
546 case AMDGPU::BI__builtin_amdgcn_fracth:
548 Intrinsic::amdgcn_fract);
549 case AMDGPU::BI__builtin_amdgcn_lerp:
551 Intrinsic::amdgcn_lerp);
552 case AMDGPU::BI__builtin_amdgcn_ubfe:
554 Intrinsic::amdgcn_ubfe);
555 case AMDGPU::BI__builtin_amdgcn_sbfe:
557 Intrinsic::amdgcn_sbfe);
558 case AMDGPU::BI__builtin_amdgcn_ballot_w32:
559 case AMDGPU::BI__builtin_amdgcn_ballot_w64: {
560 llvm::Type *ResultType = ConvertType(E->getType());
561 llvm::Value *Src = EmitScalarExpr(E->getArg(0));
562 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_ballot, { ResultType });
563 return Builder.CreateCall(F, { Src });
564 }
565 case AMDGPU::BI__builtin_amdgcn_inverse_ballot_w32:
566 case AMDGPU::BI__builtin_amdgcn_inverse_ballot_w64: {
567 llvm::Value *Src = EmitScalarExpr(E->getArg(0));
568 Function *F =
569 CGM.getIntrinsic(Intrinsic::amdgcn_inverse_ballot, {Src->getType()});
570 return Builder.CreateCall(F, {Src});
571 }
572 case AMDGPU::BI__builtin_amdgcn_tanhf:
573 case AMDGPU::BI__builtin_amdgcn_tanhh:
574 case AMDGPU::BI__builtin_amdgcn_tanh_bf16:
576 Intrinsic::amdgcn_tanh);
577 case AMDGPU::BI__builtin_amdgcn_uicmp:
578 case AMDGPU::BI__builtin_amdgcn_uicmpl:
579 case AMDGPU::BI__builtin_amdgcn_sicmp:
580 case AMDGPU::BI__builtin_amdgcn_sicmpl: {
581 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
582 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
583 llvm::Value *Src2 = EmitScalarExpr(E->getArg(2));
584
585 // FIXME-GFX10: How should 32 bit mask be handled?
586 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_icmp,
587 { Builder.getInt64Ty(), Src0->getType() });
588 return Builder.CreateCall(F, { Src0, Src1, Src2 });
589 }
590 case AMDGPU::BI__builtin_amdgcn_fcmp:
591 case AMDGPU::BI__builtin_amdgcn_fcmpf: {
592 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
593 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
594 llvm::Value *Src2 = EmitScalarExpr(E->getArg(2));
595
596 // FIXME-GFX10: How should 32 bit mask be handled?
597 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_fcmp,
598 { Builder.getInt64Ty(), Src0->getType() });
599 return Builder.CreateCall(F, { Src0, Src1, Src2 });
600 }
601 case AMDGPU::BI__builtin_amdgcn_class:
602 case AMDGPU::BI__builtin_amdgcn_classf:
603 case AMDGPU::BI__builtin_amdgcn_classh:
604 return emitFPIntBuiltin(*this, E, Intrinsic::amdgcn_class);
605 case AMDGPU::BI__builtin_amdgcn_fmed3f:
606 case AMDGPU::BI__builtin_amdgcn_fmed3h:
608 Intrinsic::amdgcn_fmed3);
609 case AMDGPU::BI__builtin_amdgcn_ds_append:
610 case AMDGPU::BI__builtin_amdgcn_ds_consume: {
611 Intrinsic::ID Intrin = BuiltinID == AMDGPU::BI__builtin_amdgcn_ds_append ?
612 Intrinsic::amdgcn_ds_append : Intrinsic::amdgcn_ds_consume;
613 Value *Src0 = EmitScalarExpr(E->getArg(0));
614 Function *F = CGM.getIntrinsic(Intrin, { Src0->getType() });
615 return Builder.CreateCall(F, { Src0, Builder.getFalse() });
616 }
617 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b64_i32:
618 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b64_v2i32:
619 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4i16:
620 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4f16:
621 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4bf16:
622 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8i16:
623 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8f16:
624 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8bf16:
625 case AMDGPU::BI__builtin_amdgcn_global_load_tr4_b64_v2i32:
626 case AMDGPU::BI__builtin_amdgcn_global_load_tr8_b64_v2i32:
627 case AMDGPU::BI__builtin_amdgcn_global_load_tr6_b96_v3i32:
628 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8i16:
629 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8f16:
630 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8bf16:
631 case AMDGPU::BI__builtin_amdgcn_ds_load_tr4_b64_v2i32:
632 case AMDGPU::BI__builtin_amdgcn_ds_load_tr8_b64_v2i32:
633 case AMDGPU::BI__builtin_amdgcn_ds_load_tr6_b96_v3i32:
634 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8i16:
635 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8f16:
636 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8bf16:
637 case AMDGPU::BI__builtin_amdgcn_ds_read_tr4_b64_v2i32:
638 case AMDGPU::BI__builtin_amdgcn_ds_read_tr8_b64_v2i32:
639 case AMDGPU::BI__builtin_amdgcn_ds_read_tr6_b96_v3i32:
640 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4f16:
641 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4bf16:
642 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4i16: {
643 Intrinsic::ID IID;
644 switch (BuiltinID) {
645 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b64_i32:
646 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b64_v2i32:
647 case AMDGPU::BI__builtin_amdgcn_global_load_tr8_b64_v2i32:
648 IID = Intrinsic::amdgcn_global_load_tr_b64;
649 break;
650 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4i16:
651 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4f16:
652 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v4bf16:
653 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8i16:
654 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8f16:
655 case AMDGPU::BI__builtin_amdgcn_global_load_tr_b128_v8bf16:
656 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8i16:
657 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8f16:
658 case AMDGPU::BI__builtin_amdgcn_global_load_tr16_b128_v8bf16:
659 IID = Intrinsic::amdgcn_global_load_tr_b128;
660 break;
661 case AMDGPU::BI__builtin_amdgcn_global_load_tr4_b64_v2i32:
662 IID = Intrinsic::amdgcn_global_load_tr4_b64;
663 break;
664 case AMDGPU::BI__builtin_amdgcn_global_load_tr6_b96_v3i32:
665 IID = Intrinsic::amdgcn_global_load_tr6_b96;
666 break;
667 case AMDGPU::BI__builtin_amdgcn_ds_load_tr4_b64_v2i32:
668 IID = Intrinsic::amdgcn_ds_load_tr4_b64;
669 break;
670 case AMDGPU::BI__builtin_amdgcn_ds_load_tr6_b96_v3i32:
671 IID = Intrinsic::amdgcn_ds_load_tr6_b96;
672 break;
673 case AMDGPU::BI__builtin_amdgcn_ds_load_tr8_b64_v2i32:
674 IID = Intrinsic::amdgcn_ds_load_tr8_b64;
675 break;
676 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8i16:
677 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8f16:
678 case AMDGPU::BI__builtin_amdgcn_ds_load_tr16_b128_v8bf16:
679 IID = Intrinsic::amdgcn_ds_load_tr16_b128;
680 break;
681 case AMDGPU::BI__builtin_amdgcn_ds_read_tr4_b64_v2i32:
682 IID = Intrinsic::amdgcn_ds_read_tr4_b64;
683 break;
684 case AMDGPU::BI__builtin_amdgcn_ds_read_tr8_b64_v2i32:
685 IID = Intrinsic::amdgcn_ds_read_tr8_b64;
686 break;
687 case AMDGPU::BI__builtin_amdgcn_ds_read_tr6_b96_v3i32:
688 IID = Intrinsic::amdgcn_ds_read_tr6_b96;
689 break;
690 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4i16:
691 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4f16:
692 case AMDGPU::BI__builtin_amdgcn_ds_read_tr16_b64_v4bf16:
693 IID = Intrinsic::amdgcn_ds_read_tr16_b64;
694 break;
695 }
696 llvm::Type *LoadTy = ConvertType(E->getType());
697 llvm::Value *Addr = EmitScalarExpr(E->getArg(0));
698 llvm::Function *F = CGM.getIntrinsic(IID, {LoadTy});
699 return Builder.CreateCall(F, {Addr});
700 }
701 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b32:
702 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b64:
703 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b128:
704 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b32:
705 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b64:
706 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b128: {
707
708 Intrinsic::ID IID;
709 switch (BuiltinID) {
710 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b32:
711 IID = Intrinsic::amdgcn_global_load_monitor_b32;
712 break;
713 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b64:
714 IID = Intrinsic::amdgcn_global_load_monitor_b64;
715 break;
716 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b128:
717 IID = Intrinsic::amdgcn_global_load_monitor_b128;
718 break;
719 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b32:
720 IID = Intrinsic::amdgcn_flat_load_monitor_b32;
721 break;
722 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b64:
723 IID = Intrinsic::amdgcn_flat_load_monitor_b64;
724 break;
725 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b128:
726 IID = Intrinsic::amdgcn_flat_load_monitor_b128;
727 break;
728 }
729
730 llvm::Type *LoadTy = ConvertType(E->getType());
731 llvm::Value *Addr = EmitScalarExpr(E->getArg(0));
732 llvm::Value *Val = EmitScalarExpr(E->getArg(1));
733 llvm::Function *F = CGM.getIntrinsic(IID, {LoadTy});
734 return Builder.CreateCall(F, {Addr, Val});
735 }
736 case AMDGPU::BI__builtin_amdgcn_cluster_load_b32:
737 case AMDGPU::BI__builtin_amdgcn_cluster_load_b64:
738 case AMDGPU::BI__builtin_amdgcn_cluster_load_b128: {
739 Intrinsic::ID IID;
740 switch (BuiltinID) {
741 case AMDGPU::BI__builtin_amdgcn_cluster_load_b32:
742 IID = Intrinsic::amdgcn_cluster_load_b32;
743 break;
744 case AMDGPU::BI__builtin_amdgcn_cluster_load_b64:
745 IID = Intrinsic::amdgcn_cluster_load_b64;
746 break;
747 case AMDGPU::BI__builtin_amdgcn_cluster_load_b128:
748 IID = Intrinsic::amdgcn_cluster_load_b128;
749 break;
750 }
752 for (int i = 0, e = E->getNumArgs(); i != e; ++i)
753 Args.push_back(EmitScalarExpr(E->getArg(i)));
754 llvm::Function *F = CGM.getIntrinsic(IID, {ConvertType(E->getType())});
755 return Builder.CreateCall(F, {Args});
756 }
757 case AMDGPU::BI__builtin_amdgcn_load_to_lds: {
758 // Should this have asan instrumentation?
760 Intrinsic::amdgcn_load_to_lds);
761 }
762 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_32x4B:
763 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_32x4B:
764 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_16x8B:
765 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_16x8B:
766 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_8x16B:
767 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_8x16B: {
768 Intrinsic::ID IID;
769 switch (BuiltinID) {
770 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_32x4B:
771 IID = Intrinsic::amdgcn_cooperative_atomic_load_32x4B;
772 break;
773 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_32x4B:
774 IID = Intrinsic::amdgcn_cooperative_atomic_store_32x4B;
775 break;
776 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_16x8B:
777 IID = Intrinsic::amdgcn_cooperative_atomic_load_16x8B;
778 break;
779 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_16x8B:
780 IID = Intrinsic::amdgcn_cooperative_atomic_store_16x8B;
781 break;
782 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_8x16B:
783 IID = Intrinsic::amdgcn_cooperative_atomic_load_8x16B;
784 break;
785 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_8x16B:
786 IID = Intrinsic::amdgcn_cooperative_atomic_store_8x16B;
787 break;
788 }
789
790 LLVMContext &Ctx = CGM.getLLVMContext();
792 // last argument is a MD string
793 const unsigned ScopeArg = E->getNumArgs() - 1;
794 for (unsigned i = 0; i != ScopeArg; ++i)
795 Args.push_back(EmitScalarExpr(E->getArg(i)));
796 StringRef Arg = cast<StringLiteral>(E->getArg(ScopeArg)->IgnoreParenCasts())
797 ->getString();
798 llvm::MDNode *MD = llvm::MDNode::get(Ctx, {llvm::MDString::get(Ctx, Arg)});
799 Args.push_back(llvm::MetadataAsValue::get(Ctx, MD));
800 // Intrinsic is typed based on the pointer AS. Pointer is always the first
801 // argument.
802 llvm::Function *F = CGM.getIntrinsic(IID, {Args[0]->getType()});
803 return Builder.CreateCall(F, {Args});
804 }
805 case AMDGPU::BI__builtin_amdgcn_get_fpenv: {
806 Function *F = CGM.getIntrinsic(Intrinsic::get_fpenv,
807 {llvm::Type::getInt64Ty(getLLVMContext())});
808 return Builder.CreateCall(F);
809 }
810 case AMDGPU::BI__builtin_amdgcn_set_fpenv: {
811 Function *F = CGM.getIntrinsic(Intrinsic::set_fpenv,
812 {llvm::Type::getInt64Ty(getLLVMContext())});
813 llvm::Value *Env = EmitScalarExpr(E->getArg(0));
814 return Builder.CreateCall(F, {Env});
815 }
816 case AMDGPU::BI__builtin_amdgcn_read_exec:
817 return EmitAMDGCNBallotForExec(*this, E, Int64Ty, Int64Ty, false);
818 case AMDGPU::BI__builtin_amdgcn_read_exec_lo:
819 return EmitAMDGCNBallotForExec(*this, E, Int32Ty, Int32Ty, false);
820 case AMDGPU::BI__builtin_amdgcn_read_exec_hi:
821 return EmitAMDGCNBallotForExec(*this, E, Int64Ty, Int64Ty, true);
822 case AMDGPU::BI__builtin_amdgcn_image_bvh_intersect_ray:
823 case AMDGPU::BI__builtin_amdgcn_image_bvh_intersect_ray_h:
824 case AMDGPU::BI__builtin_amdgcn_image_bvh_intersect_ray_l:
825 case AMDGPU::BI__builtin_amdgcn_image_bvh_intersect_ray_lh: {
826 llvm::Value *NodePtr = EmitScalarExpr(E->getArg(0));
827 llvm::Value *RayExtent = EmitScalarExpr(E->getArg(1));
828 llvm::Value *RayOrigin = EmitScalarExpr(E->getArg(2));
829 llvm::Value *RayDir = EmitScalarExpr(E->getArg(3));
830 llvm::Value *RayInverseDir = EmitScalarExpr(E->getArg(4));
831 llvm::Value *TextureDescr = EmitScalarExpr(E->getArg(5));
832
833 // The builtins take these arguments as vec4 where the last element is
834 // ignored. The intrinsic takes them as vec3.
835 RayOrigin = Builder.CreateShuffleVector(RayOrigin, RayOrigin,
836 {0, 1, 2});
837 RayDir =
838 Builder.CreateShuffleVector(RayDir, RayDir, {0, 1, 2});
839 RayInverseDir = Builder.CreateShuffleVector(RayInverseDir, RayInverseDir,
840 {0, 1, 2});
841
842 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_image_bvh_intersect_ray,
843 {NodePtr->getType(), RayDir->getType()});
844 return Builder.CreateCall(F, {NodePtr, RayExtent, RayOrigin, RayDir,
845 RayInverseDir, TextureDescr});
846 }
847 case AMDGPU::BI__builtin_amdgcn_image_bvh8_intersect_ray:
848 case AMDGPU::BI__builtin_amdgcn_image_bvh_dual_intersect_ray: {
849 Intrinsic::ID IID;
850 switch (BuiltinID) {
851 case AMDGPU::BI__builtin_amdgcn_image_bvh8_intersect_ray:
852 IID = Intrinsic::amdgcn_image_bvh8_intersect_ray;
853 break;
854 case AMDGPU::BI__builtin_amdgcn_image_bvh_dual_intersect_ray:
855 IID = Intrinsic::amdgcn_image_bvh_dual_intersect_ray;
856 break;
857 }
858 llvm::Value *NodePtr = EmitScalarExpr(E->getArg(0));
859 llvm::Value *RayExtent = EmitScalarExpr(E->getArg(1));
860 llvm::Value *InstanceMask = EmitScalarExpr(E->getArg(2));
861 llvm::Value *RayOrigin = EmitScalarExpr(E->getArg(3));
862 llvm::Value *RayDir = EmitScalarExpr(E->getArg(4));
863 llvm::Value *Offset = EmitScalarExpr(E->getArg(5));
864 llvm::Value *TextureDescr = EmitScalarExpr(E->getArg(6));
865
866 Address RetRayOriginPtr = EmitPointerWithAlignment(E->getArg(7));
867 Address RetRayDirPtr = EmitPointerWithAlignment(E->getArg(8));
868
869 llvm::Function *IntrinsicFunc = CGM.getIntrinsic(IID);
870
871 llvm::CallInst *CI = Builder.CreateCall(
872 IntrinsicFunc, {NodePtr, RayExtent, InstanceMask, RayOrigin, RayDir,
873 Offset, TextureDescr});
874
875 llvm::Value *RetVData = Builder.CreateExtractValue(CI, 0);
876 llvm::Value *RetRayOrigin = Builder.CreateExtractValue(CI, 1);
877 llvm::Value *RetRayDir = Builder.CreateExtractValue(CI, 2);
878
879 Builder.CreateStore(RetRayOrigin, RetRayOriginPtr);
880 Builder.CreateStore(RetRayDir, RetRayDirPtr);
881
882 return RetVData;
883 }
884
885 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_rtn:
886 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push4_pop1_rtn:
887 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push8_pop1_rtn:
888 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push8_pop2_rtn: {
889 Intrinsic::ID IID;
890 switch (BuiltinID) {
891 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_rtn:
892 IID = Intrinsic::amdgcn_ds_bvh_stack_rtn;
893 break;
894 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push4_pop1_rtn:
895 IID = Intrinsic::amdgcn_ds_bvh_stack_push4_pop1_rtn;
896 break;
897 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push8_pop1_rtn:
898 IID = Intrinsic::amdgcn_ds_bvh_stack_push8_pop1_rtn;
899 break;
900 case AMDGPU::BI__builtin_amdgcn_ds_bvh_stack_push8_pop2_rtn:
901 IID = Intrinsic::amdgcn_ds_bvh_stack_push8_pop2_rtn;
902 break;
903 }
904
906 for (int i = 0, e = E->getNumArgs(); i != e; ++i)
907 Args.push_back(EmitScalarExpr(E->getArg(i)));
908
909 Function *F = CGM.getIntrinsic(IID);
910 Value *Call = Builder.CreateCall(F, Args);
911 Value *Rtn = Builder.CreateExtractValue(Call, 0);
912 Value *A = Builder.CreateExtractValue(Call, 1);
913 llvm::Type *RetTy = ConvertType(E->getType());
914 Value *I0 = Builder.CreateInsertElement(PoisonValue::get(RetTy), Rtn,
915 (uint64_t)0);
916 // ds_bvh_stack_push8_pop2_rtn returns {i64, i32} but the builtin returns
917 // <2 x i64>, zext the second value.
918 if (A->getType()->getPrimitiveSizeInBits() <
919 RetTy->getScalarType()->getPrimitiveSizeInBits())
920 A = Builder.CreateZExt(A, RetTy->getScalarType());
921
922 return Builder.CreateInsertElement(I0, A, 1);
923 }
924 case AMDGPU::BI__builtin_amdgcn_mfma_scale_f32_16x16x128_f8f6f4:
925 case AMDGPU::BI__builtin_amdgcn_mfma_scale_f32_32x32x64_f8f6f4: {
926 llvm::FixedVectorType *VT = FixedVectorType::get(Builder.getInt32Ty(), 8);
927 Function *F = CGM.getIntrinsic(
928 BuiltinID == AMDGPU::BI__builtin_amdgcn_mfma_scale_f32_32x32x64_f8f6f4
929 ? Intrinsic::amdgcn_mfma_scale_f32_32x32x64_f8f6f4
930 : Intrinsic::amdgcn_mfma_scale_f32_16x16x128_f8f6f4,
931 {VT, VT});
932
934 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
935 Args.push_back(EmitScalarExpr(E->getArg(I)));
936 return Builder.CreateCall(F, Args);
937 }
938 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w32:
939 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_tied_w32:
940 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w64:
941 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_tied_w64:
942 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w32:
943 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_tied_w32:
944 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w64:
945 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_tied_w64:
946 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32:
947 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w64:
948 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w32:
949 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w64:
950 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w32:
951 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w64:
952 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w32:
953 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w64:
954 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w32_gfx12:
955 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w64_gfx12:
956 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w32_gfx12:
957 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w64_gfx12:
958 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12:
959 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w64_gfx12:
960 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12:
961 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w64_gfx12:
962 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w32_gfx12:
963 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w64_gfx12:
964 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12:
965 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w64_gfx12:
966 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12:
967 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w64_gfx12:
968 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_bf8_w32_gfx12:
969 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_bf8_w64_gfx12:
970 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_fp8_w32_gfx12:
971 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_fp8_w64_gfx12:
972 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_bf8_w32_gfx12:
973 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_bf8_w64_gfx12:
974 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x32_iu4_w32_gfx12:
975 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x32_iu4_w64_gfx12:
976 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_f16_w32:
977 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_f16_w64:
978 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf16_w32:
979 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf16_w64:
980 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x32_f16_w32:
981 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x32_f16_w64:
982 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x32_bf16_w32:
983 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x32_bf16_w64:
984 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu8_w32:
985 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu8_w64:
986 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu4_w32:
987 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu4_w64:
988 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x64_iu4_w32:
989 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x64_iu4_w64:
990 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_fp8_w32:
991 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_fp8_w64:
992 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_bf8_w32:
993 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_bf8_w64:
994 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_fp8_w32:
995 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_fp8_w64:
996 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_bf8_w32:
997 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_bf8_w64:
998 // GFX1250 WMMA builtins
999 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x4_f32:
1000 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x32_bf16:
1001 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x32_f16:
1002 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x32_f16:
1003 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x32_bf16:
1004 case AMDGPU::BI__builtin_amdgcn_wmma_bf16f32_16x16x32_bf16:
1005 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_fp8_fp8:
1006 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_fp8_bf8:
1007 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_bf8_fp8:
1008 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_bf8_bf8:
1009 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_fp8_fp8:
1010 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_fp8_bf8:
1011 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_bf8_fp8:
1012 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_bf8_bf8:
1013 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_fp8_fp8:
1014 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_fp8_bf8:
1015 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_bf8_fp8:
1016 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_bf8_bf8:
1017 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_fp8_fp8:
1018 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_fp8_bf8:
1019 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_bf8_fp8:
1020 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_bf8_bf8:
1021 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8:
1022 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_f8f6f4:
1023 case AMDGPU::BI__builtin_amdgcn_wmma_f32_32x16x128_f4:
1024 case AMDGPU::BI__builtin_amdgcn_wmma_scale_f32_16x16x128_f8f6f4:
1025 case AMDGPU::BI__builtin_amdgcn_wmma_scale16_f32_16x16x128_f8f6f4:
1026 case AMDGPU::BI__builtin_amdgcn_wmma_scale_f32_32x16x128_f4:
1027 case AMDGPU::BI__builtin_amdgcn_wmma_scale16_f32_32x16x128_f4:
1028 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x64_f16:
1029 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x64_bf16:
1030 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x64_f16:
1031 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x64_bf16:
1032 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16f32_16x16x64_bf16:
1033 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_fp8_fp8:
1034 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_fp8_bf8:
1035 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_bf8_fp8:
1036 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_bf8_bf8:
1037 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_fp8_fp8:
1038 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_fp8_bf8:
1039 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_bf8_fp8:
1040 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_bf8_bf8:
1041 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8: {
1042
1043 // These operations perform a matrix multiplication and accumulation of
1044 // the form:
1045 // D = A * B + C
1046 // We need to specify one type for matrices AB and one for matrices CD.
1047 // Sparse matrix operations can have different types for A and B as well as
1048 // an additional type for sparsity index.
1049 // Destination type should be put before types used for source operands.
1050 SmallVector<unsigned, 2> ArgsForMatchingMatrixTypes;
1051 // On GFX12, the intrinsics with 16-bit accumulator use a packed layout.
1052 // There is no need for the variable opsel argument, so always set it to
1053 // "false".
1054 bool AppendFalseForOpselArg = false;
1055 unsigned BuiltinWMMAOp;
1056 // Need return type when D and C are of different types.
1057 bool NeedReturnType = false;
1058
1059 switch (BuiltinID) {
1060 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w32:
1061 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w64:
1062 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12:
1063 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_f16_w64_gfx12:
1064 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1065 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_f16;
1066 break;
1067 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32:
1068 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w64:
1069 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12:
1070 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf16_w64_gfx12:
1071 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1072 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_bf16;
1073 break;
1074 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w32_gfx12:
1075 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w64_gfx12:
1076 AppendFalseForOpselArg = true;
1077 [[fallthrough]];
1078 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w32:
1079 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_w64:
1080 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1081 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x16_f16;
1082 break;
1083 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w32_gfx12:
1084 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w64_gfx12:
1085 AppendFalseForOpselArg = true;
1086 [[fallthrough]];
1087 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w32:
1088 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_w64:
1089 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1090 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_bf16_16x16x16_bf16;
1091 break;
1092 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_tied_w32:
1093 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x16_f16_tied_w64:
1094 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1095 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x16_f16_tied;
1096 break;
1097 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_tied_w32:
1098 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x16_bf16_tied_w64:
1099 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1100 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_bf16_16x16x16_bf16_tied;
1101 break;
1102 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w32:
1103 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w64:
1104 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12:
1105 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu8_w64_gfx12:
1106 ArgsForMatchingMatrixTypes = {4, 1}; // CD, AB
1107 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_i32_16x16x16_iu8;
1108 break;
1109 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w32:
1110 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w64:
1111 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w32_gfx12:
1112 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x16_iu4_w64_gfx12:
1113 ArgsForMatchingMatrixTypes = {4, 1}; // CD, AB
1114 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_i32_16x16x16_iu4;
1115 break;
1116 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12:
1117 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w64_gfx12:
1118 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1119 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_fp8_fp8;
1120 break;
1121 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_bf8_w32_gfx12:
1122 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_fp8_bf8_w64_gfx12:
1123 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1124 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_fp8_bf8;
1125 break;
1126 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_fp8_w32_gfx12:
1127 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_fp8_w64_gfx12:
1128 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1129 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_bf8_fp8;
1130 break;
1131 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_bf8_w32_gfx12:
1132 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x16_bf8_bf8_w64_gfx12:
1133 ArgsForMatchingMatrixTypes = {2, 0}; // CD, AB
1134 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x16_bf8_bf8;
1135 break;
1136 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x32_iu4_w32_gfx12:
1137 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x32_iu4_w64_gfx12:
1138 ArgsForMatchingMatrixTypes = {4, 1}; // CD, AB
1139 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_i32_16x16x32_iu4;
1140 break;
1141 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_f16_w32:
1142 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_f16_w64:
1143 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1144 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_f16;
1145 break;
1146 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf16_w32:
1147 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf16_w64:
1148 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1149 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_bf16;
1150 break;
1151 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x32_f16_w32:
1152 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x32_f16_w64:
1153 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1154 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x32_f16;
1155 break;
1156 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x32_bf16_w32:
1157 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x32_bf16_w64:
1158 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1159 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_bf16_16x16x32_bf16;
1160 break;
1161 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu8_w32:
1162 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu8_w64:
1163 ArgsForMatchingMatrixTypes = {4, 1, 3, 5}; // CD, A, B, Index
1164 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_i32_16x16x32_iu8;
1165 break;
1166 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu4_w32:
1167 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x32_iu4_w64:
1168 ArgsForMatchingMatrixTypes = {4, 1, 3, 5}; // CD, A, B, Index
1169 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_i32_16x16x32_iu4;
1170 break;
1171 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x64_iu4_w32:
1172 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x64_iu4_w64:
1173 ArgsForMatchingMatrixTypes = {4, 1, 3, 5}; // CD, A, B, Index
1174 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_i32_16x16x64_iu4;
1175 break;
1176 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_fp8_w32:
1177 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_fp8_w64:
1178 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1179 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_fp8;
1180 break;
1181 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_bf8_w32:
1182 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_fp8_bf8_w64:
1183 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1184 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_bf8;
1185 break;
1186 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_fp8_w32:
1187 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_fp8_w64:
1188 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1189 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_fp8;
1190 break;
1191 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_bf8_w32:
1192 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x32_bf8_bf8_w64:
1193 ArgsForMatchingMatrixTypes = {2, 0, 1, 3}; // CD, A, B, Index
1194 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_bf8;
1195 break;
1196 // GFX1250 WMMA builtins
1197 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x4_f32:
1198 ArgsForMatchingMatrixTypes = {5, 1};
1199 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x4_f32;
1200 break;
1201 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x32_bf16:
1202 ArgsForMatchingMatrixTypes = {5, 1};
1203 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x32_bf16;
1204 break;
1205 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x32_f16:
1206 ArgsForMatchingMatrixTypes = {5, 1};
1207 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x32_f16;
1208 break;
1209 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x32_f16:
1210 ArgsForMatchingMatrixTypes = {5, 1};
1211 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x32_f16;
1212 break;
1213 case AMDGPU::BI__builtin_amdgcn_wmma_bf16_16x16x32_bf16:
1214 ArgsForMatchingMatrixTypes = {5, 1};
1215 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_bf16_16x16x32_bf16;
1216 break;
1217 case AMDGPU::BI__builtin_amdgcn_wmma_bf16f32_16x16x32_bf16:
1218 NeedReturnType = true;
1219 ArgsForMatchingMatrixTypes = {1, 5};
1220 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_bf16f32_16x16x32_bf16;
1221 break;
1222 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_fp8_fp8:
1223 ArgsForMatchingMatrixTypes = {3, 0};
1224 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x64_fp8_fp8;
1225 break;
1226 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_fp8_bf8:
1227 ArgsForMatchingMatrixTypes = {3, 0};
1228 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x64_fp8_bf8;
1229 break;
1230 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_bf8_fp8:
1231 ArgsForMatchingMatrixTypes = {3, 0};
1232 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x64_bf8_fp8;
1233 break;
1234 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x64_bf8_bf8:
1235 ArgsForMatchingMatrixTypes = {3, 0};
1236 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x64_bf8_bf8;
1237 break;
1238 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_fp8_fp8:
1239 ArgsForMatchingMatrixTypes = {3, 0};
1240 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x64_fp8_fp8;
1241 break;
1242 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_fp8_bf8:
1243 ArgsForMatchingMatrixTypes = {3, 0};
1244 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x64_fp8_bf8;
1245 break;
1246 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_bf8_fp8:
1247 ArgsForMatchingMatrixTypes = {3, 0};
1248 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x64_bf8_fp8;
1249 break;
1250 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x64_bf8_bf8:
1251 ArgsForMatchingMatrixTypes = {3, 0};
1252 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x64_bf8_bf8;
1253 break;
1254 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_fp8_fp8:
1255 ArgsForMatchingMatrixTypes = {3, 0};
1256 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x128_fp8_fp8;
1257 break;
1258 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_fp8_bf8:
1259 ArgsForMatchingMatrixTypes = {3, 0};
1260 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x128_fp8_bf8;
1261 break;
1262 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_bf8_fp8:
1263 ArgsForMatchingMatrixTypes = {3, 0};
1264 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x128_bf8_fp8;
1265 break;
1266 case AMDGPU::BI__builtin_amdgcn_wmma_f16_16x16x128_bf8_bf8:
1267 ArgsForMatchingMatrixTypes = {3, 0};
1268 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f16_16x16x128_bf8_bf8;
1269 break;
1270 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_fp8_fp8:
1271 ArgsForMatchingMatrixTypes = {3, 0};
1272 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x128_fp8_fp8;
1273 break;
1274 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_fp8_bf8:
1275 ArgsForMatchingMatrixTypes = {3, 0};
1276 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x128_fp8_bf8;
1277 break;
1278 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_bf8_fp8:
1279 ArgsForMatchingMatrixTypes = {3, 0};
1280 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x128_bf8_fp8;
1281 break;
1282 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_bf8_bf8:
1283 ArgsForMatchingMatrixTypes = {3, 0};
1284 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x128_bf8_bf8;
1285 break;
1286 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8:
1287 ArgsForMatchingMatrixTypes = {4, 1};
1288 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_i32_16x16x64_iu8;
1289 break;
1290 case AMDGPU::BI__builtin_amdgcn_wmma_f32_16x16x128_f8f6f4:
1291 ArgsForMatchingMatrixTypes = {5, 1, 3};
1292 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_16x16x128_f8f6f4;
1293 break;
1294 case AMDGPU::BI__builtin_amdgcn_wmma_scale_f32_16x16x128_f8f6f4:
1295 ArgsForMatchingMatrixTypes = {5, 1, 3};
1296 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_scale_f32_16x16x128_f8f6f4;
1297 break;
1298 case AMDGPU::BI__builtin_amdgcn_wmma_scale16_f32_16x16x128_f8f6f4:
1299 ArgsForMatchingMatrixTypes = {5, 1, 3};
1300 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_scale16_f32_16x16x128_f8f6f4;
1301 break;
1302 case AMDGPU::BI__builtin_amdgcn_wmma_f32_32x16x128_f4:
1303 ArgsForMatchingMatrixTypes = {3, 0, 1};
1304 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_f32_32x16x128_f4;
1305 break;
1306 case AMDGPU::BI__builtin_amdgcn_wmma_scale_f32_32x16x128_f4:
1307 ArgsForMatchingMatrixTypes = {3, 0, 1};
1308 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_scale_f32_32x16x128_f4;
1309 break;
1310 case AMDGPU::BI__builtin_amdgcn_wmma_scale16_f32_32x16x128_f4:
1311 ArgsForMatchingMatrixTypes = {3, 0, 1};
1312 BuiltinWMMAOp = Intrinsic::amdgcn_wmma_scale16_f32_32x16x128_f4;
1313 break;
1314 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x64_f16:
1315 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1316 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x64_f16;
1317 break;
1318 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x64_bf16:
1319 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1320 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x64_bf16;
1321 break;
1322 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x64_f16:
1323 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1324 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x64_f16;
1325 break;
1326 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16_16x16x64_bf16:
1327 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1328 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_bf16_16x16x64_bf16;
1329 break;
1330 case AMDGPU::BI__builtin_amdgcn_swmmac_bf16f32_16x16x64_bf16:
1331 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1332 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_bf16f32_16x16x64_bf16;
1333 break;
1334 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_fp8_fp8:
1335 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1336 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_fp8;
1337 break;
1338 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_fp8_bf8:
1339 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1340 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_bf8;
1341 break;
1342 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_bf8_fp8:
1343 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1344 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_fp8;
1345 break;
1346 case AMDGPU::BI__builtin_amdgcn_swmmac_f32_16x16x128_bf8_bf8:
1347 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1348 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_bf8;
1349 break;
1350 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_fp8_fp8:
1351 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1352 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_fp8;
1353 break;
1354 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_fp8_bf8:
1355 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1356 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_bf8;
1357 break;
1358 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_bf8_fp8:
1359 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1360 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_fp8;
1361 break;
1362 case AMDGPU::BI__builtin_amdgcn_swmmac_f16_16x16x128_bf8_bf8:
1363 ArgsForMatchingMatrixTypes = {2, 0, 1, 3};
1364 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_bf8;
1365 break;
1366 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8:
1367 ArgsForMatchingMatrixTypes = {4, 1, 3, 5};
1368 BuiltinWMMAOp = Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8;
1369 break;
1370 }
1371
1373 for (int i = 0, e = E->getNumArgs(); i != e; ++i)
1374 Args.push_back(EmitScalarExpr(E->getArg(i)));
1375 if (AppendFalseForOpselArg)
1376 Args.push_back(Builder.getFalse());
1377
1379 if (NeedReturnType)
1380 ArgTypes.push_back(ConvertType(E->getType()));
1381 for (auto ArgIdx : ArgsForMatchingMatrixTypes)
1382 ArgTypes.push_back(Args[ArgIdx]->getType());
1383
1384 Function *F = CGM.getIntrinsic(BuiltinWMMAOp, ArgTypes);
1385 return Builder.CreateCall(F, Args);
1386 }
1387 // amdgcn workgroup size
1388 case AMDGPU::BI__builtin_amdgcn_workgroup_size_x:
1389 return EmitAMDGPUWorkGroupSize(*this, 0);
1390 case AMDGPU::BI__builtin_amdgcn_workgroup_size_y:
1391 return EmitAMDGPUWorkGroupSize(*this, 1);
1392 case AMDGPU::BI__builtin_amdgcn_workgroup_size_z:
1393 return EmitAMDGPUWorkGroupSize(*this, 2);
1394
1395 // amdgcn grid size
1396 case AMDGPU::BI__builtin_amdgcn_grid_size_x:
1397 return EmitAMDGPUGridSize(*this, 0);
1398 case AMDGPU::BI__builtin_amdgcn_grid_size_y:
1399 return EmitAMDGPUGridSize(*this, 1);
1400 case AMDGPU::BI__builtin_amdgcn_grid_size_z:
1401 return EmitAMDGPUGridSize(*this, 2);
1402
1403 // r600 intrinsics
1404 case AMDGPU::BI__builtin_r600_recipsqrt_ieee:
1405 case AMDGPU::BI__builtin_r600_recipsqrt_ieeef:
1407 Intrinsic::r600_recipsqrt_ieee);
1408 case AMDGPU::BI__builtin_amdgcn_alignbit: {
1409 llvm::Value *Src0 = EmitScalarExpr(E->getArg(0));
1410 llvm::Value *Src1 = EmitScalarExpr(E->getArg(1));
1411 llvm::Value *Src2 = EmitScalarExpr(E->getArg(2));
1412 Function *F = CGM.getIntrinsic(Intrinsic::fshr, Src0->getType());
1413 return Builder.CreateCall(F, { Src0, Src1, Src2 });
1414 }
1415 case AMDGPU::BI__builtin_amdgcn_fence: {
1417 EmitScalarExpr(E->getArg(1)), AO, SSID);
1418 FenceInst *Fence = Builder.CreateFence(AO, SSID);
1419 if (E->getNumArgs() > 2)
1421 return Fence;
1422 }
1423 case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
1424 case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
1425 case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
1426 case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
1427 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_f64:
1428 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_f32:
1429 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_v2f16:
1430 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_v2bf16:
1431 case AMDGPU::BI__builtin_amdgcn_ds_faddf:
1432 case AMDGPU::BI__builtin_amdgcn_ds_fminf:
1433 case AMDGPU::BI__builtin_amdgcn_ds_fmaxf:
1434 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_f32:
1435 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_f64:
1436 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_v2f16:
1437 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_v2f16:
1438 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_f32:
1439 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_f64:
1440 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_v2bf16:
1441 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_v2bf16:
1442 case AMDGPU::BI__builtin_amdgcn_global_atomic_fmin_f64:
1443 case AMDGPU::BI__builtin_amdgcn_global_atomic_fmax_f64:
1444 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fmin_f64:
1445 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fmax_f64: {
1446 llvm::AtomicRMWInst::BinOp BinOp;
1447 switch (BuiltinID) {
1448 case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
1449 case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
1450 BinOp = llvm::AtomicRMWInst::UIncWrap;
1451 break;
1452 case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
1453 case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
1454 BinOp = llvm::AtomicRMWInst::UDecWrap;
1455 break;
1456 case AMDGPU::BI__builtin_amdgcn_ds_faddf:
1457 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_f64:
1458 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_f32:
1459 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_v2f16:
1460 case AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_v2bf16:
1461 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_f32:
1462 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_f64:
1463 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_v2f16:
1464 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_v2f16:
1465 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_f32:
1466 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_f64:
1467 case AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_v2bf16:
1468 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_v2bf16:
1469 BinOp = llvm::AtomicRMWInst::FAdd;
1470 break;
1471 case AMDGPU::BI__builtin_amdgcn_ds_fminf:
1472 case AMDGPU::BI__builtin_amdgcn_global_atomic_fmin_f64:
1473 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fmin_f64:
1474 BinOp = llvm::AtomicRMWInst::FMin;
1475 break;
1476 case AMDGPU::BI__builtin_amdgcn_global_atomic_fmax_f64:
1477 case AMDGPU::BI__builtin_amdgcn_flat_atomic_fmax_f64:
1478 case AMDGPU::BI__builtin_amdgcn_ds_fmaxf:
1479 BinOp = llvm::AtomicRMWInst::FMax;
1480 break;
1481 }
1482
1483 Address Ptr = CheckAtomicAlignment(*this, E);
1484 Value *Val = EmitScalarExpr(E->getArg(1));
1485 llvm::Type *OrigTy = Val->getType();
1486 QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
1487
1488 bool Volatile;
1489
1490 if (BuiltinID == AMDGPU::BI__builtin_amdgcn_ds_faddf ||
1491 BuiltinID == AMDGPU::BI__builtin_amdgcn_ds_fminf ||
1492 BuiltinID == AMDGPU::BI__builtin_amdgcn_ds_fmaxf) {
1493 // __builtin_amdgcn_ds_faddf/fminf/fmaxf has an explicit volatile argument
1494 Volatile =
1495 cast<ConstantInt>(EmitScalarExpr(E->getArg(4)))->getZExtValue();
1496 } else {
1497 // Infer volatile from the passed type.
1498 Volatile =
1499 PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
1500 }
1501
1502 if (E->getNumArgs() >= 4) {
1503 // Some of the builtins have explicit ordering and scope arguments.
1505 EmitScalarExpr(E->getArg(3)), AO, SSID);
1506 } else {
1507 // Most of the builtins do not have syncscope/order arguments. For DS
1508 // atomics the scope doesn't really matter, as they implicitly operate at
1509 // workgroup scope.
1510 //
1511 // The global/flat cases need to use agent scope to consistently produce
1512 // the native instruction instead of a cmpxchg expansion.
1513 SSID = getLLVMContext().getOrInsertSyncScopeID("agent");
1514 AO = AtomicOrdering::Monotonic;
1515
1516 // The v2bf16 builtin uses i16 instead of a natural bfloat type.
1517 if (BuiltinID == AMDGPU::BI__builtin_amdgcn_ds_atomic_fadd_v2bf16 ||
1518 BuiltinID == AMDGPU::BI__builtin_amdgcn_global_atomic_fadd_v2bf16 ||
1519 BuiltinID == AMDGPU::BI__builtin_amdgcn_flat_atomic_fadd_v2bf16) {
1520 llvm::Type *V2BF16Ty = FixedVectorType::get(
1521 llvm::Type::getBFloatTy(Builder.getContext()), 2);
1522 Val = Builder.CreateBitCast(Val, V2BF16Ty);
1523 }
1524 }
1525
1526 llvm::AtomicRMWInst *RMW =
1527 Builder.CreateAtomicRMW(BinOp, Ptr, Val, AO, SSID);
1528 if (Volatile)
1529 RMW->setVolatile(true);
1530
1531 unsigned AddrSpace = Ptr.getType()->getAddressSpace();
1532 if (AddrSpace != llvm::AMDGPUAS::LOCAL_ADDRESS) {
1533 // Most targets require "amdgpu.no.fine.grained.memory" to emit the native
1534 // instruction for flat and global operations.
1535 llvm::MDTuple *EmptyMD = MDNode::get(getLLVMContext(), {});
1536 RMW->setMetadata("amdgpu.no.fine.grained.memory", EmptyMD);
1537
1538 // Most targets require "amdgpu.ignore.denormal.mode" to emit the native
1539 // instruction, but this only matters for float fadd.
1540 if (BinOp == llvm::AtomicRMWInst::FAdd && Val->getType()->isFloatTy())
1541 RMW->setMetadata("amdgpu.ignore.denormal.mode", EmptyMD);
1542 }
1543
1544 return Builder.CreateBitCast(RMW, OrigTy);
1545 }
1546 case AMDGPU::BI__builtin_amdgcn_s_sendmsg_rtn:
1547 case AMDGPU::BI__builtin_amdgcn_s_sendmsg_rtnl: {
1548 llvm::Value *Arg = EmitScalarExpr(E->getArg(0));
1549 llvm::Type *ResultType = ConvertType(E->getType());
1550 // s_sendmsg_rtn is mangled using return type only.
1551 Function *F =
1552 CGM.getIntrinsic(Intrinsic::amdgcn_s_sendmsg_rtn, {ResultType});
1553 return Builder.CreateCall(F, {Arg});
1554 }
1555 case AMDGPU::BI__builtin_amdgcn_permlane16_swap:
1556 case AMDGPU::BI__builtin_amdgcn_permlane32_swap: {
1557 // Because builtin types are limited, and the intrinsic uses a struct/pair
1558 // output, marshal the pair-of-i32 to <2 x i32>.
1559 Value *VDstOld = EmitScalarExpr(E->getArg(0));
1560 Value *VSrcOld = EmitScalarExpr(E->getArg(1));
1561 Value *FI = EmitScalarExpr(E->getArg(2));
1562 Value *BoundCtrl = EmitScalarExpr(E->getArg(3));
1563 Function *F =
1564 CGM.getIntrinsic(BuiltinID == AMDGPU::BI__builtin_amdgcn_permlane16_swap
1565 ? Intrinsic::amdgcn_permlane16_swap
1566 : Intrinsic::amdgcn_permlane32_swap);
1567 llvm::CallInst *Call =
1568 Builder.CreateCall(F, {VDstOld, VSrcOld, FI, BoundCtrl});
1569
1570 llvm::Value *Elt0 = Builder.CreateExtractValue(Call, 0);
1571 llvm::Value *Elt1 = Builder.CreateExtractValue(Call, 1);
1572
1573 llvm::Type *ResultType = ConvertType(E->getType());
1574
1575 llvm::Value *Insert0 = Builder.CreateInsertElement(
1576 llvm::PoisonValue::get(ResultType), Elt0, UINT64_C(0));
1577 llvm::Value *AsVector =
1578 Builder.CreateInsertElement(Insert0, Elt1, UINT64_C(1));
1579 return AsVector;
1580 }
1581 case AMDGPU::BI__builtin_amdgcn_bitop3_b32:
1582 case AMDGPU::BI__builtin_amdgcn_bitop3_b16:
1584 Intrinsic::amdgcn_bitop3);
1585 case AMDGPU::BI__builtin_amdgcn_make_buffer_rsrc: {
1586 // TODO: LLVM has this overloaded to allow for fat pointers, but since
1587 // those haven't been plumbed through to Clang yet, default to creating the
1588 // resource type.
1590 for (unsigned I = 0; I < 4; ++I)
1591 Args.push_back(EmitScalarExpr(E->getArg(I)));
1592 llvm::PointerType *RetTy = llvm::PointerType::get(
1593 Builder.getContext(), llvm::AMDGPUAS::BUFFER_RESOURCE);
1594 Function *F = CGM.getIntrinsic(Intrinsic::amdgcn_make_buffer_rsrc,
1595 {RetTy, Args[0]->getType()});
1596 return Builder.CreateCall(F, Args);
1597 }
1598 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b8:
1599 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b16:
1600 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b32:
1601 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b64:
1602 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b96:
1603 case AMDGPU::BI__builtin_amdgcn_raw_buffer_store_b128:
1605 *this, E, Intrinsic::amdgcn_raw_ptr_buffer_store);
1606 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b8:
1607 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b16:
1608 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b32:
1609 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b64:
1610 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b96:
1611 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b128: {
1612 llvm::Type *RetTy = nullptr;
1613 switch (BuiltinID) {
1614 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b8:
1615 RetTy = Int8Ty;
1616 break;
1617 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b16:
1618 RetTy = Int16Ty;
1619 break;
1620 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b32:
1621 RetTy = Int32Ty;
1622 break;
1623 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b64:
1624 RetTy = llvm::FixedVectorType::get(Int32Ty, /*NumElements=*/2);
1625 break;
1626 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b96:
1627 RetTy = llvm::FixedVectorType::get(Int32Ty, /*NumElements=*/3);
1628 break;
1629 case AMDGPU::BI__builtin_amdgcn_raw_buffer_load_b128:
1630 RetTy = llvm::FixedVectorType::get(Int32Ty, /*NumElements=*/4);
1631 break;
1632 }
1633 Function *F =
1634 CGM.getIntrinsic(Intrinsic::amdgcn_raw_ptr_buffer_load, RetTy);
1635 return Builder.CreateCall(
1636 F, {EmitScalarExpr(E->getArg(0)), EmitScalarExpr(E->getArg(1)),
1637 EmitScalarExpr(E->getArg(2)), EmitScalarExpr(E->getArg(3))});
1638 }
1639 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_add_i32:
1641 *this, E, Intrinsic::amdgcn_raw_ptr_buffer_atomic_add);
1642 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fadd_f32:
1643 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fadd_v2f16:
1645 *this, E, Intrinsic::amdgcn_raw_ptr_buffer_atomic_fadd);
1646 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fmin_f32:
1647 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fmin_f64:
1649 *this, E, Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmin);
1650 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fmax_f32:
1651 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_atomic_fmax_f64:
1653 *this, E, Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmax);
1654 case AMDGPU::BI__builtin_amdgcn_s_prefetch_data:
1656 *this, E, Intrinsic::amdgcn_s_prefetch_data);
1657 case Builtin::BIlogbf:
1658 case Builtin::BI__builtin_logbf: {
1659 Value *Src0 = EmitScalarExpr(E->getArg(0));
1660 Function *FrExpFunc = CGM.getIntrinsic(
1661 Intrinsic::frexp, {Src0->getType(), Builder.getInt32Ty()});
1662 CallInst *FrExp = Builder.CreateCall(FrExpFunc, Src0);
1663 Value *Exp = Builder.CreateExtractValue(FrExp, 1);
1664 Value *Add = Builder.CreateAdd(
1665 Exp, ConstantInt::getSigned(Exp->getType(), -1), "", false, true);
1666 Value *SIToFP = Builder.CreateSIToFP(Add, Builder.getFloatTy());
1667 Value *Fabs =
1668 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::fabs);
1669 Value *FCmpONE = Builder.CreateFCmpONE(
1670 Fabs, ConstantFP::getInfinity(Builder.getFloatTy()));
1671 Value *Sel1 = Builder.CreateSelect(FCmpONE, SIToFP, Fabs);
1672 Value *FCmpOEQ =
1673 Builder.CreateFCmpOEQ(Src0, ConstantFP::getZero(Builder.getFloatTy()));
1674 Value *Sel2 = Builder.CreateSelect(
1675 FCmpOEQ,
1676 ConstantFP::getInfinity(Builder.getFloatTy(), /*Negative=*/true), Sel1);
1677 return Sel2;
1678 }
1679 case Builtin::BIlogb:
1680 case Builtin::BI__builtin_logb: {
1681 Value *Src0 = EmitScalarExpr(E->getArg(0));
1682 Function *FrExpFunc = CGM.getIntrinsic(
1683 Intrinsic::frexp, {Src0->getType(), Builder.getInt32Ty()});
1684 CallInst *FrExp = Builder.CreateCall(FrExpFunc, Src0);
1685 Value *Exp = Builder.CreateExtractValue(FrExp, 1);
1686 Value *Add = Builder.CreateAdd(
1687 Exp, ConstantInt::getSigned(Exp->getType(), -1), "", false, true);
1688 Value *SIToFP = Builder.CreateSIToFP(Add, Builder.getDoubleTy());
1689 Value *Fabs =
1690 emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::fabs);
1691 Value *FCmpONE = Builder.CreateFCmpONE(
1692 Fabs, ConstantFP::getInfinity(Builder.getDoubleTy()));
1693 Value *Sel1 = Builder.CreateSelect(FCmpONE, SIToFP, Fabs);
1694 Value *FCmpOEQ =
1695 Builder.CreateFCmpOEQ(Src0, ConstantFP::getZero(Builder.getDoubleTy()));
1696 Value *Sel2 = Builder.CreateSelect(
1697 FCmpOEQ,
1698 ConstantFP::getInfinity(Builder.getDoubleTy(), /*Negative=*/true),
1699 Sel1);
1700 return Sel2;
1701 }
1702 case Builtin::BIscalbnf:
1703 case Builtin::BI__builtin_scalbnf:
1704 case Builtin::BIscalbn:
1705 case Builtin::BI__builtin_scalbn:
1707 *this, E, Intrinsic::ldexp, Intrinsic::experimental_constrained_ldexp);
1708 default:
1709 return nullptr;
1710 }
1711}
#define V(N, I)
static Value * emitBinaryExpMaybeConstrainedFPBuiltin(CodeGenFunction &CGF, const CallExpr *E, Intrinsic::ID IntrinsicID, Intrinsic::ID ConstrainedIntrinsicID)
Address CheckAtomicAlignment(CodeGenFunction &CGF, const CallExpr *E)
llvm::Value * emitBuiltinWithOneOverloadedType(clang::CodeGen::CodeGenFunction &CGF, const clang::CallExpr *E, unsigned IntrinsicID, llvm::StringRef Name="")
Definition CGBuiltin.h:63
static Intrinsic::ID getIntrinsicIDforWaveReduction(unsigned BuiltinID)
Definition AMDGPU.cpp:298
static Value * EmitAMDGCNBallotForExec(CodeGenFunction &CGF, const CallExpr *E, llvm::Type *RegisterType, llvm::Type *ValueType, bool isExecHi)
Definition AMDGPU.cpp:166
static Value * emitFPIntBuiltin(CodeGenFunction &CGF, const CallExpr *E, unsigned IntrinsicID)
Definition AMDGPU.cpp:185
TokenType getType() const
Returns the token's type, e.g.
#define X(type, name)
Definition Value.h:97
HLSLResourceBindingAttr::RegisterType RegisterType
Definition SemaHLSL.cpp:55
static QualType getPointeeType(const MemRegion *R)
Enumerates target-specific builtins in their own namespaces within namespace clang.
QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, unsigned *IntegerConstantArgs=nullptr) const
Return the type for the specified builtin.
unsigned getTargetAddressSpace(LangAS AS) const
@ GE_None
No error.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2879
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3083
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3070
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition Address.h:209
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition Address.h:204
Address CreateGEP(CodeGenFunction &CGF, Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition CGBuilder.h:296
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:112
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition CGBuilder.h:132
Address CreateAddrSpaceCast(Address Addr, llvm::Type *Ty, llvm::Type *ElementTy, const llvm::Twine &Name="")
Definition CGBuilder.h:193
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitScalarOrConstFoldImmArg(unsigned ICEArguments, unsigned Idx, const CallExpr *E)
Definition AMDGPU.cpp:258
llvm::Type * ConvertType(QualType T)
llvm::Value * EmitAMDGPUBuiltinExpr(unsigned BuiltinID, const CallExpr *E)
Definition AMDGPU.cpp:332
const TargetInfo & getTarget() const
void AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst, const CallExpr *E)
Definition AMDGPU.cpp:275
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition CGExpr.cpp:1515
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
llvm::LLVMContext & getLLVMContext()
void ProcessOrderScopeAMDGCN(llvm::Value *Order, llvm::Value *Scope, llvm::AtomicOrdering &AO, llvm::SyncScope::ID &SSID)
Definition AMDGPU.cpp:201
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
ASTContext & getContext() const
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3078
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3053
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
QualType getType() const
Definition Expr.h:144
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3328
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8369
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:323
unsigned getMaxOpenCLWorkGroupSize() const
Definition TargetInfo.h:870
llvm::CodeObjectVersionKind CodeObjectVersion
Code object version for AMDGPU.
QualType getType() const
Definition Value.cpp:237
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64