Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
Created using spr 1.3.4
  • Loading branch information
vitalybuka committed Oct 22, 2024
2 parents 741e224 + c77d8ed commit f07a39f
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===------- Optimizer/Transforms/CufOpConversion.h -------------*- C++ -*-===//
//===------- Optimizer/Transforms/CUFOpConversion.h -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -19,7 +19,7 @@ class LLVMTypeConverter;
namespace mlir {
class DataLayout;
class SymbolTable;
}
} // namespace mlir

namespace cuf {

Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace fir {
#define GEN_PASS_DECL_CHARACTERCONVERSION
#define GEN_PASS_DECL_CFGCONVERSION
#define GEN_PASS_DECL_CUFADDCONSTRUCTOR
#define GEN_PASS_DECL_CUFIMPLICITDEVICEGLOBAL
#define GEN_PASS_DECL_CUFDEVICEGLOBAL
#define GEN_PASS_DECL_CUFOPCONVERSION
#define GEN_PASS_DECL_EXTERNALNAMECONVERSION
#define GEN_PASS_DECL_MEMREFDATAFLOWOPT
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,15 @@ def AssumedRankOpConversion : Pass<"fir-assumed-rank-op", "mlir::ModuleOp"> {
];
}

def CufOpConversion : Pass<"cuf-convert", "mlir::ModuleOp"> {
def CUFOpConversion : Pass<"cuf-convert", "mlir::ModuleOp"> {
let summary = "Convert some CUF operations to runtime calls";
let dependentDialects = [
"fir::FIROpsDialect"
];
}

def CufImplicitDeviceGlobal :
Pass<"cuf-implicit-device-global", "mlir::ModuleOp"> {
def CUFDeviceGlobal :
Pass<"cuf-device-global", "mlir::ModuleOp"> {
let summary = "Flag globals used in device function with data attribute";
let dependentDialects = [
"cuf::CUFDialect"
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Optimizer/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ add_flang_library(FIRTransforms
ConstantArgumentGlobalisation.cpp
ControlFlowConverter.cpp
CUFAddConstructor.cpp
CufImplicitDeviceGlobal.cpp
CufOpConversion.cpp
CUFDeviceGlobal.cpp
CUFOpConversion.cpp
ArrayValueCopy.cpp
ExternalNameConversion.cpp
MemoryUtils.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- CufOpConversion.cpp -----------------------------------------------===//
//===-- CUFOpConversion.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -18,7 +18,7 @@
#include "mlir/Transforms/DialectConversion.h"

namespace fir {
#define GEN_PASS_DEF_CUFIMPLICITDEVICEGLOBAL
#define GEN_PASS_DEF_CUFDEVICEGLOBAL
#include "flang/Optimizer/Transforms/Passes.h.inc"
} // namespace fir

Expand All @@ -45,8 +45,7 @@ static void prepareImplicitDeviceGlobals(mlir::func::FuncOp funcOp,
}
}

class CufImplicitDeviceGlobal
: public fir::impl::CufImplicitDeviceGlobalBase<CufImplicitDeviceGlobal> {
class CUFDeviceGlobal : public fir::impl::CUFDeviceGlobalBase<CUFDeviceGlobal> {
public:
void runOnOperation() override {
mlir::Operation *op = getOperation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//===-- CufOpConversion.cpp -----------------------------------------------===//
//===-- CUFDeviceGlobal.cpp -----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "flang/Optimizer/Transforms/CufOpConversion.h"
#include "flang/Optimizer/Transforms/CUFOpConversion.h"
#include "flang/Common/Fortran.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Optimizer/CodeGen/TypeConverter.h"
Expand Down Expand Up @@ -619,7 +619,7 @@ struct CufDataTransferOpConversion
const mlir::SymbolTable &symtab;
};

class CufOpConversion : public fir::impl::CufOpConversionBase<CufOpConversion> {
class CUFOpConversion : public fir::impl::CUFOpConversionBase<CUFOpConversion> {
public:
void runOnOperation() override {
auto *ctx = &getContext();
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Fir/CUDA/cuda-implicit-device-global.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: fir-opt --split-input-file --cuf-implicit-device-global %s | FileCheck %s
// RUN: fir-opt --split-input-file --cuf-device-global %s | FileCheck %s

// Test that global used in device function are flagged with the correct
// attribute.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/MachineOperand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,8 @@ void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
}
MachineOperand::printOperandOffset(OS, getOffset());
if (!getSize().hasValue() ||
getAlign() != getSize().getValue().getKnownMinValue())
(!getSize().isZero() &&
getAlign() != getSize().getValue().getKnownMinValue()))
OS << ", align " << getAlign().value();
if (getAlign() != getBaseAlign())
OS << ", basealign " << getBaseAlign().value();
Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXTargetStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
//===----------------------------------------------------------------------===//

#include "NVPTXTargetStreamer.h"
#include "NVPTXUtilities.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Casting.h"

using namespace llvm;

Expand Down Expand Up @@ -135,3 +139,16 @@ void NVPTXTargetStreamer::emitRawBytes(StringRef Data) {
#endif
}

void NVPTXTargetStreamer::emitValue(const MCExpr *Value) {
if (Value->getKind() == MCExpr::SymbolRef) {
const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*Value);
StringRef SymName = SRE.getSymbol().getName();
if (!SymName.starts_with(".debug")) {
Streamer.emitRawText(NVPTX::getValidPTXIdentifier(SymName));
return;
}
// Fall through to the normal printing.
}
// Otherwise, print the Value normally.
MCTargetStreamer::emitValue(Value);
}
2 changes: 2 additions & 0 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXTargetStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class NVPTXTargetStreamer : public MCTargetStreamer {
///
/// This is used to emit bytes in \p Data as sequence of .byte directives.
void emitRawBytes(StringRef Data) override;
/// Makes sure that labels are mangled the same way as the actual symbols.
void emitValue(const MCExpr *Value) override;
};

class NVPTXAsmTargetStreamer : public NVPTXTargetStreamer {
Expand Down
29 changes: 4 additions & 25 deletions llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
//===----------------------------------------------------------------------===//

#include "NVPTX.h"
#include "llvm/ADT/StringExtras.h"
#include "NVPTXUtilities.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include <string>

using namespace llvm;

Expand All @@ -35,11 +33,8 @@ class NVPTXAssignValidGlobalNames : public ModulePass {
NVPTXAssignValidGlobalNames() : ModulePass(ID) {}

bool runOnModule(Module &M) override;

/// Clean up the name to remove symbols invalid in PTX.
std::string cleanUpName(StringRef Name);
};
}
} // namespace

char NVPTXAssignValidGlobalNames::ID = 0;

Expand All @@ -58,34 +53,18 @@ bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
// Note: this does not create collisions - if setName is asked to set the
// name to something that already exists, it adds a proper postfix to
// avoid collisions.
GV.setName(cleanUpName(GV.getName()));
GV.setName(NVPTX::getValidPTXIdentifier(GV.getName()));
}
}

// Do the same for local functions.
for (Function &F : M.functions())
if (F.hasLocalLinkage())
F.setName(cleanUpName(F.getName()));
F.setName(NVPTX::getValidPTXIdentifier(F.getName()));

return true;
}

std::string NVPTXAssignValidGlobalNames::cleanUpName(StringRef Name) {
std::string ValidName;
raw_string_ostream ValidNameStream(ValidName);
for (char C : Name) {
// While PTX also allows '%' at the start of identifiers, LLVM will throw a
// fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
if (isAlnum(C) || C == '_' || C == '$') {
ValidNameStream << C;
} else {
ValidNameStream << "_$_";
}
}

return ValidNameStream.str();
}

ModulePass *llvm::createNVPTXAssignValidGlobalNamesPass() {
return new NVPTXAssignValidGlobalNames();
}
3 changes: 2 additions & 1 deletion llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "NVPTXUtilities.h"
#include "NVPTX.h"
#include "NVPTXTargetMachine.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
Expand All @@ -33,7 +34,7 @@
namespace llvm {

namespace {
typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
typedef std::map<std::string, std::vector<unsigned>> key_val_pair_t;
typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;

struct AnnotationCache {
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Target/NVPTX/NVPTXUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define LLVM_LIB_TARGET_NVPTX_NVPTXUTILITIES_H

#include "NVPTX.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
Expand Down Expand Up @@ -84,6 +85,19 @@ bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM);
bool Isv2x16VT(EVT VT);

namespace NVPTX {
inline std::string getValidPTXIdentifier(StringRef Name) {
std::string ValidName;
ValidName.reserve(Name.size() + 4);
for (char C : Name)
// While PTX also allows '%' at the start of identifiers, LLVM will throw a
// fatal error for '%' in symbol names in MCSymbol::print. Exclude for now.
if (isAlnum(C) || C == '_' || C == '$')
ValidName.push_back(C);
else
ValidName.append({'_', '$', '_'});

return ValidName;
}

inline std::string OrderingToString(Ordering Order) {
switch (Order) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/CodeGen/WebAssembly/externref-globalget.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types | FileCheck %s
; Test for MIR printing of reference types in other address spaces. This should
; not error out.
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types -print-after=finalize-isel | FileCheck %s

%externref = type ptr addrspace(10) ;; addrspace 10 is nonintegral

Expand Down
42 changes: 42 additions & 0 deletions llvm/test/DebugInfo/NVPTX/debug-ptx-symbols.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; RUN: llc < %s -march=nvptx64 -mcpu=sm_60 | FileCheck %s
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_60 | %ptxas-verify %}
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"

; Verify that the symbols with the characters illegal in PTX get appropriately mangled.
@__PRETTY_FUNCTION__._Z3foov = private unnamed_addr constant [11 x i8] c"void foo()\00", align 1, !dbg !0
; '.' gets replaced with `_$_`.
; CHECK: .global .align 1 .b8 __PRETTY_FUNCTION___$__Z3foov[11] = {118, 111, 105, 100, 32, 102, 111, 111, 40, 41};

; .debug* section names are special and are allowed to have the leading dot.
; CHECK-DAG: .section .debug_abbrev
; CHECK-DAG: .section .debug_info
; CHECK-DAG: .b32 .debug_abbrev
; CHECK-DAG: .b32 .debug_line
; CHECK-DAG: .section .debug_macinfo

; .. but the symbol name must be mangled the same way here as it was at the definition point.
; CHECK-DAG: .b64 __PRETTY_FUNCTION___$__Z3foov
;

!llvm.dbg.cu = !{!8}
!llvm.module.flags = !{!10, !11, !12}

!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(scope: null, file: !2, line: 1, type: !3, isLocal: true, isDefinition: true)
!2 = !DIFile(filename: "<stdin>", directory: "/usr/local/google/home/tra/work/llvm/build/release+assert+zapcc/dbg-dot")
!3 = !DICompositeType(tag: DW_TAG_array_type, baseType: !4, size: 88, elements: !6)
!4 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !5)
!5 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!6 = !{!7}
!7 = !DISubrange(count: 11)
!8 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !2, producer: "clang version 20.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !9, splitDebugInlining: false, nameTableKind: None)
!9 = !{!0}
!10 = !{i32 2, !"Debug Info Version", i32 3}
!11 = !{i32 1, !"wchar_size", i32 4}
!12 = !{i32 4, !"nvvm-reflect-ftz", i32 0}
!13 = !{!"clang version 20.0.0git"}
!14 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !2, file: !2, line: 1, type: !15, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !8)
!15 = !DISubroutineType(types: !16)
!16 = !{null}
!17 = !DILocation(line: 1, column: 56, scope: !14)

0 comments on commit f07a39f

Please sign in to comment.