Background: The benefit of sildenafil in patients with advanced idiopathic pulmonary fibrosis (IPF) at risk of poor outcomes from pulmonary hypertension, whether already present or likely to develop, is uncertain. We aimed to assess the efficacy and safety of sildenafil added to pirfenidone versus placebo added to pirfenidone for 52 weeks in patients with advanced IPF and at risk of group 3 pulmonary hypertension. Methods: We did a multicentre, international, double-blind, randomised, placebo-controlled, phase 2b study at 56 university clinics, research hospitals, and tertiary sites in Canada, Europe (Belgium, Czech Republic, Germany, Greece, Hungary, Italy, the Netherlands, Spain, and Turkey), Israel, and Africa (Egypt and South Africa). Eligible patients (aged 40–80 years) had advanced IPF (carbon monoxide diffusing capacity ≤40% predicted at screening), and were at risk of group 3 pulmonary hypertension (mean pulmonary artery pressure of ≥20 mm Hg with pulmonary artery wedge pressure of ≤15 mm Hg on previous right-heart catheterisation, or intermediate or high probability of group 3 pulmonary hypertension on echocardiography as defined by the 2015 European Society of Cardiology and European Respiratory Society guidelines). Patients were randomly assigned 1:1 to oral sildenafil tablets (20 mg three times daily) or placebo, both in addition to oral pirfenidone capsules (801 mg three times daily), using a validated interactive voice-based or web-based response system with permuted block randomisation, stratified by previous right-heart catheterisation (yes or no) and forced expiratory volume in 1 s to forced vital capacity ratio (<0·8 or ≥0·8). The composite primary endpoint was disease progression, defined as either a relevant decline in 6-min walk distance, respiratory-related admission to hospital, or all-cause mortality, after 52 weeks and was assessed in the intention-to-treat population; safety was assessed in all patients who received at least one dose of the study drug. This trial is registered with ClinicalTrials.gov, NCT02951429, and is no longer recruiting. The 11-month safety follow-up is ongoing. Findings: Between Jan 13, 2017, and Aug 30, 2018, 247 patients were screened for eligibility, 177 of whom were randomly assigned to a treatment group (n=88 sildenafil; n=89 placebo) and were assessed for the primary outcome. There was no difference in the proportion of patients with disease progression over 52 weeks between the sildenafil (64 [73%] of 88 patients) and placebo groups (62 [70%] of 89 patients; between-group difference 3·06% [95% CI −11·30 to 17·97]; p=0·65). Serious treatment-emergent adverse events were reported in 54 (61%) patients in the sildenafil group and 55 (62%) patients in the placebo group. Treatment-emergent adverse events leading to mortality occurred in 22 (25%) patients in the sildenafil group and 26 (29%) in the placebo group. Interpretation: Addition of sildenafil to pirfenidone did not provide a treatment benefit versus pirfenidone plus placebo up to 52 weeks in patients with advanced IPF and risk of pulmonary hypertension. No new safety signals were identified with either treatment. Although the absence of a beneficial treatment effect suggests that sildenafil is not an appropriate treatment in the overall population, further research is required to establish if specific subgroups of patients with IPF might benefit from sildenafil. Funding: F Hoffmann-La Roche.