Skip to content

Commit

Permalink
Contributing Intel Tofino compiler backend to p4c (#4964)
Browse files Browse the repository at this point in the history
* Add licenses and top level scripts

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files in top level directory

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/arch for handling tna/t2na/v1model architectures

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/arch/psa for handling psa to tna translation

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/arch/tna

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/common

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/pragma

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/control-plane for bfrt support

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/driver for compiler driver

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/ir for tofino-specific IR

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/lib

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/logging

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/mau

Core algorithms to allocate resource in MAU

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/p4include

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde

Core algorithms for Parser and Deparser allocation

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/clot

clot stands for Checksum-Length-Offset-Tag

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/lowered, common and mirror

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/phv (part 1)

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/phv/analysis (part 2)

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/phv/pragma (part 3)

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/phv/* (part 4)

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/parde/phv/v2 (part 5)

V2 algorithm for phv allocation

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-p4c/test/gtest

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/bf-utils for dynamic_hash library

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/compiler-interfaces

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Add files under backends/tofino/scripts for misc scripts

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Changes to other backend to fix compilation error

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Changes to top level CMakeLists.txt to add tofino backend

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* fix compilation issues

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* CI script fixes

- fix ci-build.sh
- add pyinstaller to requirement.txt
- link ${P4C_LIB_DEPS} to bf_gtest_support target
- link ${P4C_LIB_DEPS} to tofinobackend target

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Fixes for coding-style issues

- clang-format
- black format
- isort errors
- ifdefs name fixes

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* switch ENABLE_TOFINO to OFF by default

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* try 20.04 build

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* add FindZ3.cmake for bf-p4c

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Fix p4c driver issues

- remove duplicated bmv2/psa.p4 and dpdk/psa.p4
- remove calling bfas in p4c-driver
- disable a few driver tests

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Addressing review comments

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Update licenses to Apache 2.0

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* Create new test runner for tofino backend

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* reenable driver tests

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

* address review comments

Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
Signed-off-by: Han Wang <[email protected]>

---------

Signed-off-by: Han Wang <[email protected]>
Co-authored-by: Glen Gibb <[email protected]>
Co-authored-by: Prathima Kotikalapudi <[email protected]>
Co-authored-by: Han Wang <[email protected]>
  • Loading branch information
4 people authored Oct 25, 2024
1 parent 0916051 commit 2605767
Show file tree
Hide file tree
Showing 980 changed files with 332,078 additions and 84 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci-test-debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ jobs:
run: sudo -E ctest --output-on-failure --schedule-random
working-directory: ./build

# Build with GCC and test Tofino backend on Ubuntu 22.04.
test-ubuntu22-tofino:
runs-on: ubuntu-22.04
env:
CTEST_PARALLEL_LEVEL: 4
IMAGE_TYPE: test
BUILD_GENERATOR: Ninja
ENABLE_GTESTS: ON
ENABLE_TOFINO: ON
ENABLE_BMV2: OFF
ENABLE_EBPF: OFF
ENABLE_UBPF: OFF
ENABLE_DPDK: OFF
ENABLE_P4TC: OFF
ENABLE_P4FMT: OFF
ENABLE_P4TEST: OFF
ENABLE_P4C_GRAPHS: OFF
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- name: ccache
uses: hendrikmuhs/ccache-action@v1
with:
key: test-${{ runner.os }}-gcc
max-size: 1000M

- name: Build (Ubuntu 22.04, GCC)
run: |
tools/ci-build.sh
# Build with GCC and test P4C on Ubuntu 20.04.
test-ubuntu20:
name: test-ubuntu20 (Unity ${{ matrix.unity }}, GTest ${{ matrix.gtest }})
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ docs/doxygen/p4c-doxygen-warn.log

# Local Documentation build theme files
docs/doxygen/awesome_css/

# thirdparty
backends/tofino/third_party/
32 changes: 19 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

# This is the CMake version supplied by Ubuntu 20.04.
cmake_minimum_required (VERSION 3.16.3 FATAL_ERROR)
Expand Down Expand Up @@ -39,6 +40,7 @@ OPTION (ENABLE_BMV2 "Build the BMV2 backend (required for the full test suite)"
OPTION (ENABLE_EBPF "Build the EBPF backend (required for the full test suite)" ON)
OPTION (ENABLE_UBPF "Build the uBPF backend (required for the full test suite)" ON)
OPTION (ENABLE_DPDK "Build the DPDK backend (required for the full test suite)" ON)
OPTION (ENABLE_TOFINO "Build the TOFINO backend (required for the full test suite)" OFF)
OPTION (ENABLE_P4TC "Build the P4TC backend" ON)
OPTION (ENABLE_P4FMT "Build the P4FMT backend" ON)
OPTION (ENABLE_P4TEST "Build the P4Test backend (required for the full test suite)" ON)
Expand Down Expand Up @@ -374,6 +376,7 @@ include_directories (
${P4C_SOURCE_DIR}
${P4C_BINARY_DIR}
${P4C_SOURCE_DIR}/extensions
${P4C_SOURCE_DIR}/backends/tofino ## FIXME
)
add_definitions (-DCONFIG_PREFIX="${CMAKE_INSTALL_PREFIX}")
add_definitions (-DCONFIG_PKGDATADIR="${CMAKE_INSTALL_PREFIX}/${P4C_ARTIFACTS_OUTPUT_DIRECTORY}")
Expand Down Expand Up @@ -487,6 +490,9 @@ endif ()
if (ENABLE_UBPF)
add_subdirectory (backends/ubpf)
endif ()
if (ENABLE_TOFINO)
add_subdirectory (backends/tofino)
endif ()
if (ENABLE_GTESTS)
add_subdirectory (test)
endif ()
Expand Down
1 change: 1 addition & 0 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ filter=-runtime/references
filter=-readability/todo
filter=-readability/function
filter=-readability/casting
filter=-readability/fn_size
linelength=100
# Do not use 'p4c' as the cpp header guard prefix.
root=.
Expand Down
8 changes: 6 additions & 2 deletions backends/common/psaProgramStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ limitations under the License.

namespace P4 {

enum gress_t { INGRESS, EGRESS };
enum psa_gress_t {
INGRESS,
EGRESS,
};

enum block_t {
PARSER,
PIPELINE,
Expand All @@ -32,7 +36,7 @@ enum block_t {
class PsaProgramStructure : public PortableProgramStructure {
public:
/// Architecture related information.
ordered_map<const IR::Node *, std::pair<gress_t, block_t>> block_type;
ordered_map<const IR::Node *, std::pair<psa_gress_t, block_t>> block_type;

public:
PsaProgramStructure(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
Expand Down
68 changes: 0 additions & 68 deletions backends/p4tools/cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,3 @@ function(add_p4tools_executable target source)
install(TARGETS ${target} RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY})
endfunction(add_p4tools_executable)

macro(p4tools_obtain_z3)
option(TOOLS_USE_PREINSTALLED_Z3 "Look for a preinstalled version of Z3 instead of installing a prebuilt binary using FetchContent." OFF)

if(TOOLS_USE_PREINSTALLED_Z3)
# We need a fairly recent version of Z3.
set(Z3_MIN_VERSION "4.8.14")
# But 4.12+ is currently broken with libGC
set(Z3_MAX_VERSION_EXCL "4.12")
find_package(Z3 ${Z3_MIN_VERSION} REQUIRED)

if(NOT DEFINED Z3_VERSION_STRING OR ${Z3_VERSION_STRING} VERSION_LESS ${Z3_MIN_VERSION})
message(FATAL_ERROR "The minimum required Z3 version is ${Z3_MIN_VERSION}. Has ${Z3_VERSION_STRING}.")
endif()
if(${Z3_VERSION_STRING} VERSION_GREATER_EQUAL ${Z3_MAX_VERSION_EXCL})
message(FATAL_ERROR "The Z3 version has to be lower than ${Z3_MAX_VERSION_EXCL} (the latter currently does no work with libGC). Has ${Z3_VERSION_STRING}.")
endif()
# Set variables for later consumption.
set(P4TOOLS_Z3_LIB z3::z3)
set(P4TOOLS_Z3_INCLUDE_DIR ${Z3_INCLUDE_DIR})
else()
# Pull in a specific version of Z3 and link against it.
set(P4TOOLS_Z3_VERSION "4.11.2")
message("Fetching Z3 version ${P4TOOLS_Z3_VERSION} for P4Tools...")

# Determine platform to fetch pre-built Z3
if (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(Z3_ARCH "x64")
if (APPLE)
set(Z3_PLATFORM_SUFFIX "osx-10.16")
set(Z3_ZIP_HASH "a56b6c40d9251a963aabe1f15731dd88ad1cb801d0e7b16e45f8b232175e165c")
elseif (UNIX)
set(Z3_PLATFORM_SUFFIX "glibc-2.31")
set(Z3_ZIP_HASH "9d0f70e61e82b321f35e6cad1343615d2dead6f2c54337a24293725de2900fb6")
else()
message(FATAL_ERROR "Unsupported system platform")
endif()
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
set(Z3_ARCH "arm64")
if (APPLE)
set(Z3_PLATFORM_SUFFIX "osx-11.0")
set(Z3_ZIP_HASH "c021f381fa3169b1f7fb3b4fae81a1d1caf0dd8aa4aa773f4ab9d5e28c6657a4")
else()
message(FATAL_ERROR "Unsupported system platform")
endif()
else()
message(FATAL_ERROR "Unsupported system processor")
endif()

# Print out download state while setting up Z3.
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET})
set(FETCHCONTENT_QUIET OFF)
fetchcontent_declare(
z3
URL https://github.com/Z3Prover/z3/releases/download/z3-${P4TOOLS_Z3_VERSION}/z3-${P4TOOLS_Z3_VERSION}-${Z3_ARCH}-${Z3_PLATFORM_SUFFIX}.zip
URL_HASH SHA256=${Z3_ZIP_HASH}
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
)
fetchcontent_makeavailable(z3)
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
message("Done with setting up Z3 for P4Tools.")

# Other projects may also pull in Z3.
# We have to make sure we only include our local version with P4Tools.
set(P4TOOLS_Z3_LIB ${z3_SOURCE_DIR}/bin/libz3${CMAKE_STATIC_LIBRARY_SUFFIX})
set(P4TOOLS_Z3_INCLUDE_DIR ${z3_SOURCE_DIR}/include)
endif()
endmacro(p4tools_obtain_z3)
1 change: 1 addition & 0 deletions backends/p4tools/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Handle the Z3 installation with this macro. Users have the option to supply their own Z3.
include(${P4C_SOURCE_DIR}/cmake/Z3.cmake)
p4tools_obtain_z3()

# Generate version information.
Expand Down
Loading

0 comments on commit 2605767

Please sign in to comment.