From 52c39396151978ca946e2a80d9118c8672bace14 Mon Sep 17 00:00:00 2001 From: Momchil Velikov Date: Wed, 17 Jul 2019 10:53:13 +0000 Subject: [PATCH] [TableGen] Do not set ReadNone attribute on intrinsics with side effects If an intrinsic is defined without outputs, but having side effects, it still can be removed completely from the program. This patch makes TableGen not set Attribute::ReadNone for intrinsics which are declared with IntrHasSideEffects. Differential Revision: https://reviews.llvm.org/D64414 llvm-svn: 366312 --- llvm/test/TableGen/intrin-side-effects.td | 39 ++++++++++++++++++++++++++++++ llvm/utils/TableGen/CodeGenDAGPatterns.cpp | 2 +- llvm/utils/TableGen/IntrinsicEmitter.cpp | 4 ++- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 llvm/test/TableGen/intrin-side-effects.td diff --git a/llvm/test/TableGen/intrin-side-effects.td b/llvm/test/TableGen/intrin-side-effects.td new file mode 100644 index 0000000..7588855 --- /dev/null +++ b/llvm/test/TableGen/intrin-side-effects.td @@ -0,0 +1,39 @@ +// RUN: llvm-tblgen -gen-intrinsic-impl -I %p/../../include %s | FileCheck %s + +// Get the minimum blurb necessary to process ... +include "llvm/CodeGen/ValueTypes.td" +include "llvm/CodeGen/SDNodeProperties.td" + +class LLVMType { + ValueType VT = vt; + int isAny = 0; +} + +def llvm_i32_ty : LLVMType; + +class IntrinsicProperty; +def IntrNoMem : IntrinsicProperty; +def IntrHasSideEffects : IntrinsicProperty; + + +class Intrinsic ret_types, + list param_types = [], + list intr_properties = [], + string name = "", + list sd_properties = []> : SDPatternOperator { + string LLVMName = name; + string TargetPrefix = ""; + list RetTypes = ret_types; + list ParamTypes = param_types; + list IntrProperties = intr_properties; + let Properties = sd_properties; + + bit isTarget = 0; +} + +// ... this intrinsic. +def int_random_gen : Intrinsic<[llvm_i32_ty], [], [IntrNoMem, IntrHasSideEffects]>; + +// CHECK: 1, // llvm.random.gen +// CHECK: case 1: +// CHECK-NEXT: Atts[] = {Attribute::NoUnwind} diff --git a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp index a0e8696..c8f710d 100644 --- a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp +++ b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp @@ -2807,7 +2807,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit, // chain. if (Int.IS.RetVTs.empty()) Operator = getDAGPatterns().get_intrinsic_void_sdnode(); - else if (Int.ModRef != CodeGenIntrinsic::NoMem) + else if (Int.ModRef != CodeGenIntrinsic::NoMem || Int.hasSideEffects) // Has side-effects, requires chain. Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); else // Otherwise, no chain. diff --git a/llvm/utils/TableGen/IntrinsicEmitter.cpp b/llvm/utils/TableGen/IntrinsicEmitter.cpp index bcb8af2..6bcdc3d 100644 --- a/llvm/utils/TableGen/IntrinsicEmitter.cpp +++ b/llvm/utils/TableGen/IntrinsicEmitter.cpp @@ -685,7 +685,7 @@ void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, } if (!intrinsic.canThrow || - intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem || + (intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem && !intrinsic.hasSideEffects) || intrinsic.isNoReturn || intrinsic.isCold || intrinsic.isNoDuplicate || intrinsic.isConvergent || intrinsic.isSpeculatable) { OS << " const Attribute::AttrKind Atts[] = {"; @@ -727,6 +727,8 @@ void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, switch (intrinsic.ModRef) { case CodeGenIntrinsic::NoMem: + if (intrinsic.hasSideEffects) + break; if (addComma) OS << ","; OS << "Attribute::ReadNone"; -- 2.7.4