Skip to content

[AMDGPU] Implement variadic functions by IR lowering #93362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/lib/CodeGen/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {

Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
QualType Ty) const {
llvm_unreachable("AMDGPU does not support varargs");
const bool IsIndirect = false;
const bool AllowHigherAlign = false;
return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
getContext().getTypeInfoInChars(Ty),
CharUnits::fromQuantity(4), AllowHigherAlign);
}

ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
Expand Down
478 changes: 478 additions & 0 deletions clang/test/CodeGen/voidptr-vaarg.c

Large diffs are not rendered by default.

181 changes: 181 additions & 0 deletions clang/test/CodeGenCXX/inline-then-fold-variadics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --function-signature
// REQUIRES: webassembly-registered-target

// Simple calls to known variadic functions that are completely elided when
// optimisations are on This is a functional check that the expand-variadic pass
// is consistent with clang's va_arg handling

// When expand-variadics is added to the default pipeline, clang -O1 will
// suffice here -Wno-varargs avoids warning second argument to 'va_start' is not
// the last named parameter

// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -Wno-varargs -O1 -emit-llvm -o - | opt - -S --passes='module(expand-variadics,default<O1>)' --expand-variadics-override=optimize -o - | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need REQUIRES: wasm-registered-target


#include <stdarg.h>
#include <stdint.h>

template <typename X, typename Y> static X first(...) {
va_list va;
__builtin_va_start(va, 0);
X r = va_arg(va, X);
va_end(va);
return r;
}

template <typename X, typename Y> static Y second(...) {
va_list va;
__builtin_va_start(va, 0);
va_arg(va, X);
Y r = va_arg(va, Y);
va_end(va);
return r;
}

extern "C" {

// CHECK-LABEL: define {{[^@]+}}@first_pair_i32
// CHECK-SAME: (i32 noundef returned [[X:%.*]], i32 noundef [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[X]]
//
int first_pair_i32(int x, int y) { return first<int, int>(x, y); }

// CHECK-LABEL: define {{[^@]+}}@second_pair_i32
// CHECK-SAME: (i32 noundef [[X:%.*]], i32 noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[Y]]
//
int second_pair_i32(int x, int y) { return second<int, int>(x, y); }

// CHECK-LABEL: define {{[^@]+}}@first_pair_f64
// CHECK-SAME: (double noundef returned [[X:%.*]], double noundef [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret double [[X]]
//
double first_pair_f64(double x, double y) {
return first<double, double>(x, y);
}

// CHECK-LABEL: define {{[^@]+}}@second_pair_f64
// CHECK-SAME: (double noundef [[X:%.*]], double noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret double [[Y]]
//
double second_pair_f64(double x, double y) {
return second<double, double>(x, y);
}
}

extern "C" {

// CHECK-LABEL: define {{[^@]+}}@first_i32_f64
// CHECK-SAME: (i32 noundef returned [[X:%.*]], double noundef [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[X]]
//
int first_i32_f64(int x, double y) { return first<int, double>(x, y); }

// CHECK-LABEL: define {{[^@]+}}@second_i32_f64
// CHECK-SAME: (i32 noundef [[X:%.*]], double noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret double [[Y]]
//
double second_i32_f64(int x, double y) { return second<int, double>(x, y); }

// CHECK-LABEL: define {{[^@]+}}@first_f64_i32
// CHECK-SAME: (double noundef returned [[X:%.*]], i32 noundef [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret double [[X]]
//
double first_f64_i32(double x, int y) { return first<double, int>(x, y); }

// CHECK-LABEL: define {{[^@]+}}@second_f64_i32
// CHECK-SAME: (double noundef [[X:%.*]], i32 noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[Y]]
//
int second_f64_i32(double x, int y) { return second<double, int>(x, y); }
}

extern "C" {
typedef uint64_t ulong2 __attribute__((__vector_size__(16), __aligned__(16)));

// CHECK-LABEL: define {{[^@]+}}@first_i32_ulong2
// CHECK-SAME: (i32 noundef returned [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[X]]
//
int first_i32_ulong2(int x, ulong2 *y) { return first<int, ulong2>(x, *y); }

// CHECK-LABEL: define {{[^@]+}}@second_i32_ulong2
// CHECK-SAME: (i32 noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]], ptr nocapture noundef writeonly [[R:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = load <2 x i64>, ptr [[Y]], align 16, !tbaa [[TBAA2:![0-9]+]]
// CHECK-NEXT: store <2 x i64> [[TMP0]], ptr [[R]], align 16, !tbaa [[TBAA2]]
// CHECK-NEXT: ret void
//
void second_i32_ulong2(int x, ulong2 *y, ulong2 *r) {
*r = second<int, ulong2>(x, *y);
}

// CHECK-LABEL: define {{[^@]+}}@first_ulong2_i32
// CHECK-SAME: (ptr nocapture noundef readonly [[X:%.*]], i32 noundef [[Y:%.*]], ptr nocapture noundef writeonly [[R:%.*]]) local_unnamed_addr #[[ATTR1]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = load <2 x i64>, ptr [[X]], align 16, !tbaa [[TBAA2]]
// CHECK-NEXT: store <2 x i64> [[TMP0]], ptr [[R]], align 16, !tbaa [[TBAA2]]
// CHECK-NEXT: ret void
//
void first_ulong2_i32(ulong2 *x, int y, ulong2 *r) {
*r = first<ulong2, int>(*x, y);
}

// CHECK-LABEL: define {{[^@]+}}@second_ulong2_i32
// CHECK-SAME: (ptr nocapture noundef readonly [[X:%.*]], i32 noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[Y]]
//
int second_ulong2_i32(ulong2 *x, int y) { return second<ulong2, int>(*x, y); }
}

// ascending alignment
typedef struct {
char c;
short s;
int i;
long l;
float f;
double d;
} asc;

extern "C" {

// CHECK-LABEL: define {{[^@]+}}@first_i32_asc
// CHECK-SAME: (i32 noundef returned [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[X]]
//
int first_i32_asc(int x, asc *y) { return first<int, asc>(x, *y); }

// CHECK-LABEL: define {{[^@]+}}@second_i32_asc
// CHECK-SAME: (i32 noundef [[X:%.*]], ptr nocapture noundef readonly [[Y:%.*]], ptr nocapture noundef writeonly [[R:%.*]]) local_unnamed_addr #[[ATTR1]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: tail call void @llvm.memmove.p0.p0.i32(ptr noundef nonnull align 8 dereferenceable(24) [[R]], ptr noundef nonnull align 1 dereferenceable(24) [[Y]], i32 24, i1 false)
// CHECK-NEXT: ret void
//
void second_i32_asc(int x, asc *y, asc *r) { *r = second<int, asc>(x, *y); }

// CHECK-LABEL: define {{[^@]+}}@first_asc_i32
// CHECK-SAME: (ptr nocapture noundef readonly [[X:%.*]], i32 noundef [[Y:%.*]], ptr nocapture noundef writeonly [[R:%.*]]) local_unnamed_addr #[[ATTR1]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: tail call void @llvm.memmove.p0.p0.i32(ptr noundef nonnull align 8 dereferenceable(24) [[R]], ptr noundef nonnull align 1 dereferenceable(24) [[X]], i32 24, i1 false)
// CHECK-NEXT: ret void
//
void first_asc_i32(asc *x, int y, asc *r) { *r = first<asc, int>(*x, y); }

// CHECK-LABEL: define {{[^@]+}}@second_asc_i32
// CHECK-SAME: (ptr nocapture noundef readonly [[X:%.*]], i32 noundef returned [[Y:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i32 [[Y]]
//
int second_asc_i32(asc *x, int y) { return second<asc, int>(*x, y); }
}
4 changes: 4 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fflush
libc.src.stdio.ftell
libc.src.stdio.clearerr
libc.src.stdio.sprintf
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
libc.src.stdio.vsnprintf
libc.src.stdio.puts
libc.src.stdio.fopen
libc.src.stdio.fclose
Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ add_libc_test(
libc.src.__support.uint128
)

# The GPU does not support varargs currently.
if(NOT LIBC_TARGET_OS_IS_GPU)
# NVPTX does not support varargs currently.
if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
add_libc_test(
arg_list_test
SUITE
Expand Down
9 changes: 9 additions & 0 deletions llvm/include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,15 @@ class CallBase : public Instruction {
return Attrs.getParamStackAlignment(ArgNo);
}

/// Extract the byref type for a call or parameter.
Type *getParamByRefType(unsigned ArgNo) const {
if (auto *Ty = Attrs.getParamByRefType(ArgNo))
return Ty;
if (const Function *F = getCalledFunction())
return F->getAttributes().getParamByRefType(ArgNo);
return nullptr;
}

/// Extract the byval type for a call or parameter.
Type *getParamByValType(unsigned ArgNo) const {
if (auto *Ty = Attrs.getParamByValType(ArgNo))
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void initializeExpandLargeDivRemLegacyPassPass(PassRegistry&);
void initializeExpandMemCmpLegacyPassPass(PassRegistry &);
void initializeExpandPostRAPass(PassRegistry&);
void initializeExpandReductionsPass(PassRegistry&);
void initializeExpandVariadicsPass(PassRegistry &);
void initializeExpandVectorPredicationPass(PassRegistry &);
void initializeExternalAAWrapperPassPass(PassRegistry&);
void initializeFEntryInserterPass(PassRegistry&);
Expand Down
40 changes: 40 additions & 0 deletions llvm/include/llvm/Transforms/IPO/ExpandVariadics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===- ExpandVariadics.h - expand variadic functions ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_IPO_EXPANDVARIADICS_H
#define LLVM_TRANSFORMS_IPO_EXPANDVARIADICS_H

#include "llvm/IR/PassManager.h"

namespace llvm {

class Module;
class ModulePass;
class OptimizationLevel;

enum class ExpandVariadicsMode {
Unspecified, // Use the implementation defaults
Disable, // Disable the pass entirely
Optimize, // Optimise without changing ABI
Lowering, // Change variadic calling convention
};

class ExpandVariadicsPass : public PassInfoMixin<ExpandVariadicsPass> {
const ExpandVariadicsMode Mode;

public:
// Operates under passed mode unless overridden on commandline
ExpandVariadicsPass(ExpandVariadicsMode Mode);

PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

ModulePass *createExpandVariadicsPass(ExpandVariadicsMode);

} // end namespace llvm

#endif // LLVM_TRANSFORMS_IPO_EXPANDVARIADICS_H
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
#include "llvm/Transforms/IPO/ElimAvailExtern.h"
#include "llvm/Transforms/IPO/EmbedBitcodePass.h"
#include "llvm/Transforms/IPO/ExpandVariadics.h"
#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
#include "llvm/Transforms/IPO/FunctionAttrs.h"
#include "llvm/Transforms/IPO/FunctionImport.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ MODULE_PASS("dot-callgraph", CallGraphDOTPrinterPass())
MODULE_PASS("dxil-upgrade", DXILUpgradePass())
MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass())
MODULE_PASS("extract-blocks", BlockExtractorPass({}, false))
MODULE_PASS("expand-variadics", ExpandVariadicsPass(ExpandVariadicsMode::Disable))
MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
MODULE_PASS("function-import", FunctionImportPass())
MODULE_PASS("globalopt", GlobalOptPass())
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/AlwaysInliner.h"
#include "llvm/Transforms/IPO/ExpandVariadics.h"
#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/Transforms/IPO/Internalize.h"
#include "llvm/Transforms/Scalar.h"
Expand Down Expand Up @@ -992,6 +993,10 @@ void AMDGPUPassConfig::addIRPasses() {
if (isPassEnabled(EnableImageIntrinsicOptimizer))
addPass(createAMDGPUImageIntrinsicOptimizerPass(&TM));

// This can be disabled by passing ::Disable here or on the command line
// with --expand-variadics-override=disable.
addPass(createExpandVariadicsPass(ExpandVariadicsMode::Lowering));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a kill switch cl::opt for the pass in the AMDGPUTargetMachine before landing, in case it has unforeseen issues. It'd be easier to default-disable while fixing, rather than revert a big patch :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also paranoid enough to want that, it's '--expand-variadics-override=disable' from the command line or changing the ::Lowering in that line to ::Disable. Commenting out the addPass also kills it, but would cause the lit pipeline tests to fail. I'll put a comment above that pass saying this.


// Function calls are not supported, so make sure we inline everything.
addPass(createAMDGPUAlwaysInlinePass());
addPass(createAlwaysInlinerLegacyPass());
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/IPO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_llvm_component_library(LLVMipo
DeadArgumentElimination.cpp
ElimAvailExtern.cpp
EmbedBitcodePass.cpp
ExpandVariadics.cpp
ExtractGV.cpp
ForceFunctionAttrs.cpp
FunctionAttrs.cpp
Expand Down
Loading
Loading